-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrepo.py
More file actions
95 lines (70 loc) · 2.62 KB
/
repo.py
File metadata and controls
95 lines (70 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
This module contains a class that allows us to interact with the
repository of interest.
"""
import tempfile
import shutil
import subprocess
import os
class Repo(object):
"""
Interact with a repository: attributes, extract a copy, cleanup.
This could maybe be re-written as a context manger so the cleanup happens
automatically.
"""
def __init__(self, name, url, rtype="git"):
"""
name - Name of the project
url - url of the project
rtype - type of the repository. This will mostly be git.
"""
self.name = name
self.url = url
self.rtype = rtype
self.local_resources = []
self.extracted = False
self.file_list = []
self.tmpdir = None
def extract_local_copy(self):
"""Extract a local copy of the repository"""
self.tmpdir = tempfile.mkdtemp()
self.local_resources.append(self.tmpdir)
if self.rtype is "git":
extract_cmd = "git clone {url} {odir}".format(url=self.url,
odir=self.tmpdir)
else:
# We could implement SVN here, a quick svn export would do.
raise NotImplemented
try:
subprocess.check_call(extract_cmd.split())
except subprocess.CalledProcessError:
raise IOError("Unable to extract a local copy of repository")
else:
self.extracted = True
def has(self, filename):
"""
Does the repository have a file matching a particular name? If it does
then return the filename, otherwise return False.
"""
if not self.extracted:
self.extract_local_copy()
if not self.file_list:
self._get_filelist()
for f in self.file_list:
if filename in f:
return f
return False
def _get_filelist(self):
"""Just get a list of the files in the repo."""
if not self.extracted:
self.extract_local_copy()
for root, dirs, files in os.walk(self.tmpdir, topdown=True):
for name in files:
self.file_list.append(os.path.join(root, name))
def cleanup(self):
"""Remove any local resources"""
for resource in self.local_resources:
try:
shutil.rmtree(resource)
except:
print "Unable to remove: {}".format(resource)