import urllib
import shutil
import os
+import os.path
from wizard import app, command, shell
def main(argv, baton):
options, args = parse_args(argv, baton)
- appname, _, version = args[0].partition("-")
- application = app.applications()[appname]
- url = application.download(version)
- base = os.path.basename(url)
- sh = shell.Shell()
- # try to clear out the directory
- if not options.force:
- _, _, ref = open(".git/HEAD").read().partition(' ')
- if ref != "refs/heads/pristine":
- raise Exception("Not on pristine branch (override with --force)")
- try:
- sh.call("git", "status")
- raise Exception("Working copy is dirty (override with --force)")
- except shell.CallError:
- pass
- files = sh.eval("git", "ls-files", "-o")
- if files:
- raise Exception("Unversioned files exist, refusing to remove (override with --force)")
- for f in os.listdir(os.getcwd()):
- if f == ".git": continue
- try:
- os.unlink(f)
- except OSError as e:
- shutil.rmtree(f)
- with open(base, "w") as outfile:
- infile = urllib.urlopen(url)
- shutil.copyfileobj(infile, outfile)
+ check_directory(options)
+ if not os.path.exists(args[0]):
+ appname, _, version = args[0].partition("-")
+ application = app.getApplication(appname)
+ url = application.download(version)
+ base = os.path.basename(url)
+ with open(base, "w") as outfile:
+ infile = urllib.urlopen(url)
+ shutil.copyfileobj(infile, outfile)
+ shell.call("tar", "xf", base)
+ os.unlink(base)
+ else:
+ base = args[0]
+ shell.call("tar", "xf", base)
# extract the files, but be smart: if only one directory is output,
# move the contents of that directory here
- sh.call("tar", "xf", base)
- os.unlink(base)
items = [f for f in os.listdir(os.getcwd()) if f[0] != "."]
if len(items) == 1 and os.path.isdir(items[0]):
os.rename(items[0], "_wizard_source")
- sh.call("cp", "-R", "_wizard_source/.", ".")
+ shell.call("cp", "-R", "_wizard_source/.", ".")
shutil.rmtree("_wizard_source")
# populate empty directories with blank files
for dirpath, dirnames, filenames in os.walk(os.getcwd()):
if "/.git" in dirpath: continue
if not filenames and not dirnames:
open(os.path.join(dirpath, ".preserve-dir"), "w").write("")
- sh.call("git", "add", ".")
+ # stage all changes
+ shell.call("git", "add", ".")
+ for f in shell.call("git", "ls-files", "-d", "-z")[0].split("\0"):
+ if f != "":
+ shell.call("git", "rm", "--", f)
def parse_args(argv, baton):
usage = """usage: %prog prepare-pristine APP-VERSION
elif len(args) > 1:
parser.error("too many arguments")
return options, args
+
+def check_directory(options):
+ if not os.path.exists(".git"):
+ raise Exception("Not in root directory of Git repository")
+ files = shell.eval("git", "ls-files", "-o")
+ if files:
+ raise Exception("Unversioned files exist, refusing to remove (override with --force)")
+ try:
+ shell.call("git", "rev-parse", "HEAD")
+ _, _, ref = open(".git/HEAD").read().rstrip().partition(' ')
+ if not options.force:
+ if ref != "refs/heads/pristine" and os.path.exists(os.path.join(".git", ref)):
+ raise Exception("Not on pristine branch (override with --force)")
+ if shell.eval("git", "ls-files", "-m").strip() != "":
+ raise Exception("Working copy is dirty (override with --force)")
+ for f in os.listdir(os.getcwd()):
+ if f == ".git": continue
+ try:
+ os.unlink(f)
+ except OSError as e:
+ shutil.rmtree(f)
+ except shell.CallError:
+ # We're on a git repo with no HEAD
+ pass