def print_view(request): error_message = None success_message = None if request.method == 'POST': form = PrintForm(request.user, request.POST, request.FILES) if form.is_valid(): try: execute(settings.PRINTING_COMMAND, stdin=form.cleaned_data['file']) except ExecuteError as e: error_message = six.text_type(e) else: success_message = _("File has been printed.") else: form = PrintForm(request.user) return TemplateResponse( request, 'printing/print.html', { 'form': form, 'success_message': success_message, 'error_message': error_message, }, )
def _process_checkers(self): checker_prefix = os.path.join(self.rootdir, 'prog', self.short_name + 'chk') checker = None source_candidates = [ checker_prefix + '.cpp', checker_prefix + '.c', checker_prefix + '.pas', ] for source in source_candidates: if os.path.isfile(source): logger.info('%s: compiling checker', self.filename) execute(['make', self.short_name + 'chk.e'], cwd=os.path.join(self.rootdir, 'prog')) break exe_candidates = [ checker_prefix + '.e', checker_prefix + '.sh', ] for exe in exe_candidates: if os.path.isfile(exe): checker = exe instance = OutputChecker.objects.get(problem=self.problem) if checker: instance.exe_file.save(os.path.basename(checker), File(open(checker, 'rb'))) else: instance.exe_file = None instance.save()
def generate_all(dir): generate_from_template( dir, 'settings.py', { '__DIR__': dir, '__SECRET__': str(uuid.uuid4()), '__TIMEZONE__': get_timezone(), }) settings = {} settings_py = os.path.join(dir, 'settings.py') execfile(settings_py, globals(), settings) media_root = settings['MEDIA_ROOT'] os.mkdir(media_root) static_root = settings['STATIC_ROOT'] os.mkdir(static_root) os.mkdir(os.path.join(dir, 'logs')) os.mkdir(os.path.join(dir, 'pidfiles')) virtual_env = os.environ.get('VIRTUAL_ENV', '') user = pwd.getpwuid(os.getuid())[0] group = grp.getgrgid(os.getgid())[0] manage_py = os.path.join(dir, 'manage.py') generate_from_template( dir, 'manage.py', { '__DIR__': dir, '__PYTHON_EXECUTABLE__': sys.executable, '__VIRTUAL_ENV__': virtual_env, }) os.chmod(manage_py, 0755) generate_from_template(dir, 'supervisord.conf', { '__USER__': user, }) generate_from_template(dir, 'wsgi.py', { '__DIR__': dir, '__VIRTUAL_ENV__': virtual_env, }) generate_from_template( dir, 'apache-site.conf', { '__DIR__': dir, '__STATIC_URL__': settings['STATIC_URL'], '__STATIC_ROOT__': settings['STATIC_ROOT'], '__USER__': user, '__GROUP__': group, }) # Having DJANGO_SETTINGS_MODULE here would probably cause collectstatic to # run with wrong settings. os.environ.pop('DJANGO_SETTINGS_MODULE', None) execute([sys.executable, manage_py, 'collectstatic', '--noinput'], capture_output=False)
def generate_all(dir): generate_from_template(dir, 'settings.py', { '__DIR__': dir, '__SECRET__': str(uuid.uuid4()), '__TIMEZONE__': get_timezone(), }) settings = {} settings_py = os.path.join(dir, 'settings.py') execfile(settings_py, globals(), settings) media_root = settings['MEDIA_ROOT'] os.mkdir(media_root) static_root = settings['STATIC_ROOT'] os.mkdir(static_root) os.mkdir(os.path.join(dir, 'logs')) os.mkdir(os.path.join(dir, 'pidfiles')) virtual_env = os.environ.get('VIRTUAL_ENV', '') user = pwd.getpwuid(os.getuid())[0] group = grp.getgrgid(os.getgid())[0] manage_py = os.path.join(dir, 'manage.py') generate_from_template(dir, 'manage.py', { '__DIR__': dir, '__PYTHON_EXECUTABLE__': sys.executable, '__VIRTUAL_ENV__': virtual_env, }) os.chmod(manage_py, 0755) generate_from_template(dir, 'supervisord.conf', { '__USER__': user, }) generate_from_template(dir, 'wsgi.py', { '__DIR__': dir, '__VIRTUAL_ENV__': virtual_env, }) generate_from_template(dir, 'apache-site.conf', { '__DIR__': dir, '__STATIC_URL__': settings['STATIC_URL'], '__STATIC_ROOT__': settings['STATIC_ROOT'], '__USER__': user, '__GROUP__': group, }) # Having DJANGO_SETTINGS_MODULE here would probably cause collectstatic to # run with wrong settings. os.environ.pop('DJANGO_SETTINGS_MODULE', None) execute([sys.executable, manage_py, 'collectstatic', '--noinput'], capture_output=False)
def generate_all(dir): generate_from_template( dir, "settings.py", {"__DIR__": dir, "__SECRET__": str(uuid.uuid4()), "__TIMEZONE__": get_timezone()} ) settings = {} settings_py = os.path.join(dir, "settings.py") execfile(settings_py, globals(), settings) media_root = settings["MEDIA_ROOT"] os.mkdir(media_root) static_root = settings["STATIC_ROOT"] os.mkdir(static_root) os.mkdir(os.path.join(dir, "logs")) os.mkdir(os.path.join(dir, "pidfiles")) virtual_env = os.environ.get("VIRTUAL_ENV", "") user = pwd.getpwuid(os.getuid())[0] group = grp.getgrgid(os.getgid())[0] manage_py = os.path.join(dir, "manage.py") generate_from_template( dir, "manage.py", {"__DIR__": dir, "__PYTHON_EXECUTABLE__": sys.executable, "__VIRTUAL_ENV__": virtual_env} ) os.chmod(manage_py, 0755) generate_from_template(dir, "supervisord.conf", {"__USER__": user}) generate_from_template(dir, "wsgi.py", {"__DIR__": dir, "__VIRTUAL_ENV__": virtual_env}) generate_from_template( dir, "apache-site.conf", { "__DIR__": dir, "__STATIC_URL__": settings["STATIC_URL"], "__STATIC_ROOT__": settings["STATIC_ROOT"], "__USER__": user, "__GROUP__": group, }, ) # Having DJANGO_SETTINGS_MODULE here would probably cause collectstatic to # run with wrong settings. os.environ.pop("DJANGO_SETTINGS_MODULE", None) execute([sys.executable, manage_py, "collectstatic", "--noinput"], capture_output=False)
def main(): infile = os.path.realpath(os.path.abspath(sys.argv[1])) staticfiles_root = os.path.realpath(os.path.abspath(settings.STATIC_ROOT)) staticfiles_root = staticfiles_root.rstrip(os.sep) + os.sep if infile.startswith(staticfiles_root): execute(['lessc'] + sys.argv[1:], capture_output=False) return tmpdir = tempfile.mkdtemp() try: os.chdir(tmpdir) staticfiles_path = find_staticfiles_path(infile) collect_sources(tmpdir, staticfiles_path) execute(['lessc', staticfiles_path] + sys.argv[2:], capture_output=False) finally: shutil.rmtree(tmpdir)
def _compile_docs(self, docdir): # fancyheadings.sty looks like a rarely available LaTeX package... src_fancyheadings = os.path.join(os.path.dirname(__file__), 'files', 'fancyheadings.sty') dst_fancyheadings = os.path.join(docdir, 'fancyheadings.sty') if not os.path.isfile(dst_fancyheadings): shutil.copyfile(src_fancyheadings, dst_fancyheadings) # Extract sinol.cls and oilogo.*, but do not overwrite if they # already exist (-k). sinol_cls_tgz = os.path.join(os.path.dirname(__file__), 'files', 'sinol-cls.tgz') execute(['tar', '-C', docdir, '-kzxf', sinol_cls_tgz], cwd=docdir) try: execute('make', cwd=docdir) except ExecuteError: logger.warning("%s: failed to compile statement", self.filename, exc_info=True)
def _compile_latex_docs(self, docdir): # fancyheadings.sty looks like a rarely available LaTeX package... src_fancyheadings = os.path.join(os.path.dirname(__file__), 'files', 'fancyheadings.sty') dst_fancyheadings = os.path.join(docdir, 'fancyheadings.sty') if not os.path.isfile(dst_fancyheadings): shutil.copyfile(src_fancyheadings, dst_fancyheadings) # Extract sinol.cls and oilogo.*, but do not overwrite if they # already exist (-k). sinol_cls_tgz = os.path.join(os.path.dirname(__file__), 'files', 'sinol-cls.tgz') execute(['tar', '-C', docdir, '-kzxf', sinol_cls_tgz], cwd=docdir) try: execute('make', cwd=docdir) except ExecuteError: logger.warning("%s: failed to compile statement", self.filename, exc_info=True)
def print_view(request, contest_id): error_message = None success_message = None if request.method == 'POST': form = PrintForm(request.user, request.POST, request.FILES) if form.is_valid(): try: execute(settings.PRINTING_COMMAND, stdin=form.cleaned_data['file']) except ExecuteError as e: error_message = unicode(e) else: success_message = _("File has been printed.") else: form = PrintForm(request.user) return TemplateResponse(request, 'printing/print.html', {'form': form, 'success_message': success_message, 'error_message': error_message})
def generate_pdf(tex_code, filename, extra_args=[], num_passes=3): # Create temporary file and folder tmp_folder = tempfile.mkdtemp() try: tex_filename = 'doc.tex' tex_path = os.path.join(tmp_folder, tex_filename) with codecs.open(tex_path, 'w', 'utf-8') as f: f.write(tex_code) command = ['pdflatex'] command.extend(extra_args) command.append(tex_filename) for _i in range(num_passes): execute(command, cwd=tmp_folder) # Get PDF file contents pdf_file = open(os.path.splitext(tex_path)[0] + '.pdf') return stream_file(File(pdf_file), filename) finally: shutil.rmtree(tmp_folder)
def generate_pdf(tex_code, filename, extra_args=[], num_passes=3): # Create temporary file and folder tmp_folder = tempfile.mkdtemp() try: tex_filename = 'doc.tex' tex_path = os.path.join(tmp_folder, tex_filename) with codecs.open(tex_path, 'w', 'utf-8') as f: f.write(tex_code) command = ['pdflatex'] command.extend(extra_args) command.append(tex_filename) for _i in xrange(num_passes): execute(command, cwd=tmp_folder) # Get PDF file contents pdf_file = open(os.path.splitext(tex_path)[0] + '.pdf') return stream_file(File(pdf_file), filename) finally: shutil.rmtree(tmp_folder)
def _compile_using_make(self, command, cwd, suffix): renv = None if glob.glob( os.path.join(self.rootdir, 'prog', '%s%s.*' % (self.short_name, suffix))): logger.info("%s: %s", self.filename, command) renv = {} if not cwd: cwd = self.rootdir renv['stdout'] = execute('make %s' % (command), cwd=cwd) logger.info(renv['stdout']) return renv
def _compile_using_make(self, command, cwd, suffix): renv = None if glob.glob(os.path.join(self.rootdir, 'prog', '%s%s.*' % (self.short_name, suffix))): logger.info("%s: %s", self.filename, command) renv = {} if not cwd: cwd = self.rootdir renv['stdout'] = execute('make %s' % (command), cwd=cwd).decode('utf-8', 'replace') logger.info(renv['stdout']) return renv
def _generate_tests(self): if glob.glob( os.path.join(self.rootdir, 'prog', '%singen.*' % (self.short_name, ))): logger.info('%s: ingen', self.filename) execute('make ingen', cwd=self.rootdir) else: logger.info('%s: no ingen in package', self.filename) if glob.glob( os.path.join(self.rootdir, 'prog', '%sinwer.*' % (self.short_name, ))): logger.info('%s: inwer', self.filename) execute('make inwer', cwd=self.rootdir) else: logger.info('%s: no inwer in package', self.filename) indir = os.path.join(self.rootdir, 'in') outdir = os.path.join(self.rootdir, 'out') for test in os.listdir(indir): basename = os.path.splitext(test)[0] if not os.path.exists(os.path.join(outdir, basename + '.out')): logger.info('%s: outgen', self.filename) execute('make outgen', cwd=self.rootdir) break
def _find_and_compile(self, suffix, command=None, cwd=None, log_on_failure=True, out_name=None): renv = None if not command: command = suffix if self.use_make: if glob.glob(os.path.join(self.rootdir, 'prog', '%s%s.*' % (self.short_name, suffix))): logger.info("%s: %s", self.filename, command) renv = {} if not cwd: cwd = self.rootdir renv['stdout'] = execute('make %s' % (command), cwd=cwd) logger.info(renv['stdout']) else: name = self.short_name + suffix choices = (getattr(settings, 'SUBMITTABLE_EXTENSIONS', {})). \ values() lang_exts = [] for ch in choices: lang_exts.extend(ch) source = None for ext in lang_exts: src = os.path.join(self.rootdir, 'prog', '%s.%s' % (name, ext)) if os.path.isfile(src): source = src extension = ext break if source: renv = self._compile(source, name, extension, out_name) logger.info("%s: %s", self.filename, command) if not renv and log_on_failure: logger.info("%s: no %s in package", self.filename, command) return renv
def _generate_tests(self): logger.info('%s: ingen', self.filename) execute('make ingen', cwd=self.rootdir) if glob.glob(os.path.join(self.rootdir, 'prog', '%sinwer.*' % (self.short_name,))): logger.info('%s: inwer', self.filename) execute('make inwer', cwd=self.rootdir) else: logger.info('%s: no inwer in package', self.filename) indir = os.path.join(self.rootdir, 'in') outdir = os.path.join(self.rootdir, 'out') for test in os.listdir(indir): basename = os.path.splitext(test)[0] if not os.path.exists(os.path.join(outdir, basename + '.out')): logger.info('%s: outgen', self.filename) execute('make outgen', cwd=self.rootdir) break
def test_echo(self): self.assertEqual("foo\n", execute("echo foo"))
def test_cwd(self): self.assertEqual( execute(['cat', os.path.basename(__file__)], cwd=os.path.dirname(__file__)), open(__file__, 'rb').read())
def handle(self, *args, **options): print("--- Downloading Manifest ...", file=self.stdout) try: manifest_url = options['manifest_url'] manifest = (six.moves.urllib.request.urlopen( manifest_url).read().decode('utf-8')) manifest = manifest.strip().splitlines() except Exception as e: raise CommandError("Error downloading manifest: %s" % (e, )) print("--- Looking for license ...", file=self.stdout) try: license_url = six.moves.urllib.parse.urljoin( manifest_url, 'LICENSE') license = (six.moves.urllib.request.urlopen( license_url).read().decode('utf-8')) if not options['license_agreement']: self.display_license(license) except six.moves.urllib.error.HTTPError as e: if e.code != 404: raise args = options['sandboxes'] if not args: args = manifest print("--- Preparing ...", file=self.stdout) urls = [] cached_args = [] for arg in args: basename = arg + '.tar.gz' if options['cache_dir']: path = os.path.join(options['cache_dir'], basename) if os.path.isfile(path): cached_args.append(arg) continue if arg not in manifest: raise CommandError("Sandbox '%s' not available (not in " "Manifest)" % (arg, )) urls.append(six.moves.urllib.parse.urljoin(manifest_url, basename)) filetracker = get_client() download_dir = options['download_dir'] if not os.path.exists(download_dir): os.makedirs(download_dir) try: execute([options['wget'], '--version']) except ExecuteError: raise CommandError("Wget not working. Please specify a working " "Wget binary using --wget option.") if len(urls) > 0: print("--- Downloading sandboxes ...", file=self.stdout) quiet_flag = ['-nv'] if options['quiet'] else [] execute( [options['wget'], '-N', '-i', '-'] + quiet_flag, stdin='\n'.join(urls).encode('utf-8'), capture_output=False, cwd=download_dir, ) print("--- Saving sandboxes to the Filetracker ...", file=self.stdout) for arg in args: basename = arg + '.tar.gz' if arg in cached_args: local_file = os.path.join(options['cache_dir'], basename) else: local_file = os.path.join(download_dir, basename) print(" ", basename, file=self.stdout) filetracker.put_file('/sandboxes/' + basename, local_file) if arg not in cached_args: os.unlink(local_file) try: os.rmdir(download_dir) except OSError: print( "--- Done, but couldn't remove the " "downloads directory.", file=self.stdout, ) else: print("--- Done.", file=self.stdout)
def test_env(self): self.assertEqual('bar\n', execute('echo $foo', env = {'foo': 'bar'}))
def test_ignored_error_list(self): execute('exit 12', errors_to_ignore=(12,15,16)) execute('exit 15', errors_to_ignore=(12,15,16)) with self.assertRaises(ExecuteError): execute('return 14')
def test_error(self): with self.assertRaises(ExecuteError): execute('exit 12') # random error code
print >> self.stdout, "--- Preparing ..." urls = [] for arg in args: if arg not in manifest: raise CommandError("Sandbox '%s' not available (not in " "Manifest)" % (arg, )) urls.append(urlparse.urljoin(manifest_url, arg + '.tar.gz')) filetracker = get_client() dir = options['download_dir'] if not os.path.exists(dir): os.makedirs(dir) try: execute([options['wget'], '--version']) except ExecuteError: raise CommandError("Wget not working. Please specify a working " "Wget binary using --wget option.") print >> self.stdout, "--- Downloading sandboxes ..." execute([options['wget'], '-N', '-i', '-'], stdin='\n'.join(urls), capture_output=False, cwd=dir) print >> self.stdout, "--- Saving sandboxes to the Filetracker ..." for arg in args: basename = arg + '.tar.gz' local_file = os.path.join(dir, basename) print >> self.stdout, " ", basename
def test_echo_with_list_command(self): self.assertEqual("-n test\n", execute(['echo', '-n test']))
if os.path.isfile(path): cached_args.append(arg) continue if arg not in manifest: raise CommandError("Sandbox '%s' not available (not in " "Manifest)" % (arg,)) urls.append(urlparse.urljoin(manifest_url, basename)) filetracker = get_client() download_dir = options['download_dir'] if not os.path.exists(download_dir): os.makedirs(download_dir) try: execute([options['wget'], '--version']) except ExecuteError: raise CommandError("Wget not working. Please specify a working " "Wget binary using --wget option.") if len(urls) > 0: print >> self.stdout, "--- Downloading sandboxes ..." quiet_flag = ['-nv'] if options['quiet'] else [] execute([options['wget'], '-N', '-i', '-'] + quiet_flag, stdin='\n'.join(urls), capture_output=False, cwd=download_dir) print >> self.stdout, "--- Saving sandboxes to the Filetracker ..." for arg in args: basename = arg + '.tar.gz'
def generate_all(dir, verbose): generate_from_template( dir, 'settings.py', { '__CONFIG_VERSION__': str(INSTALLATION_CONFIG_VERSION), '__DIR__': dir, '__SECRET__': str(uuid.uuid4()), '__TIMEZONE__': get_timezone(), }) settings = {} settings_py = os.path.join(dir, 'settings.py') six.exec_(compile(open(settings_py).read(), settings_py, 'exec'), globals(), settings) media_root = settings['MEDIA_ROOT'] os.mkdir(media_root) static_root = settings['STATIC_ROOT'] os.mkdir(static_root) os.mkdir(os.path.join(dir, 'logs')) os.mkdir(os.path.join(dir, 'pidfiles')) virtual_env = os.environ.get('VIRTUAL_ENV', '') user = pwd.getpwuid(os.getuid())[0] manage_py = os.path.join(dir, 'manage.py') generate_from_template(dir, 'manage.py', { '__DIR__': dir, '__PYTHON_EXECUTABLE__': sys.executable, '__VIRTUAL_ENV__': virtual_env, }, mode=0o755) generate_from_template(dir, 'supervisord.conf', { '__USER__': user, }) generate_from_template(dir, 'wsgi.py', { '__DIR__': dir, '__VIRTUAL_ENV__': virtual_env, }) generate_from_template(dir, 'start_supervisor.sh', { '__DIR__': dir, '__VIRTUAL_ENV__': virtual_env, }, mode=0o755) generate_from_template( dir, 'apache-site.conf', { '__DIR__': dir, '__STATIC_URL__': settings['STATIC_URL'], '__STATIC_ROOT__': settings['STATIC_ROOT'], }) generate_from_template( dir, 'nginx-site.conf', { '__DIR__': dir, '__STATIC_URL__': settings['STATIC_URL'], '__STATIC_ROOT__': settings['STATIC_ROOT'], }) # Having DJANGO_SETTINGS_MODULE here would probably cause collectstatic to # run with wrong settings. os.environ.pop('DJANGO_SETTINGS_MODULE', None) # Let's silence collectstatic a bit - a ton of (normally) useless logs # happen from it print('Collecting static files...', file=sys.stderr) cmd = [sys.executable, manage_py, 'collectstatic', '--noinput'] if not verbose: cmd += ['-v', '0'] execute(cmd, capture_output=False)
def generate_all(dir, verbose): generate_from_template(dir, 'settings.py', { '__CONFIG_VERSION__': str(INSTALLATION_CONFIG_VERSION), '__DIR__': dir, '__SECRET__': str(uuid.uuid4()), '__TIMEZONE__': get_timezone(), }) settings = {} settings_py = os.path.join(dir, 'settings.py') execfile(settings_py, globals(), settings) media_root = settings['MEDIA_ROOT'] os.mkdir(media_root) static_root = settings['STATIC_ROOT'] os.mkdir(static_root) os.mkdir(os.path.join(dir, 'logs')) os.mkdir(os.path.join(dir, 'pidfiles')) virtual_env = os.environ.get('VIRTUAL_ENV', '') user = pwd.getpwuid(os.getuid())[0] manage_py = os.path.join(dir, 'manage.py') generate_from_template(dir, 'manage.py', { '__DIR__': dir, '__PYTHON_EXECUTABLE__': sys.executable, '__VIRTUAL_ENV__': virtual_env, }, mode=0755) generate_from_template(dir, 'supervisord.conf', { '__USER__': user, }) generate_from_template(dir, 'wsgi.py', { '__DIR__': dir, '__VIRTUAL_ENV__': virtual_env, }) generate_from_template(dir, 'start_supervisor.sh', { '__DIR__': dir, '__VIRTUAL_ENV__': virtual_env, }, mode=0755) generate_from_template(dir, 'apache-site.conf', { '__DIR__': dir, '__STATIC_URL__': settings['STATIC_URL'], '__STATIC_ROOT__': settings['STATIC_ROOT'], }) generate_from_template(dir, 'nginx-site.conf', { '__DIR__': dir, '__STATIC_URL__': settings['STATIC_URL'], '__STATIC_ROOT__': settings['STATIC_ROOT'], }) # Having DJANGO_SETTINGS_MODULE here would probably cause collectstatic to # run with wrong settings. os.environ.pop('DJANGO_SETTINGS_MODULE', None) # Let's silence collectstatic a bit - a ton of (normally) useless logs # happen from it print >> sys.stderr, 'Collecting static files...' cmd = [sys.executable, manage_py, 'collectstatic', '--noinput'] if not verbose: cmd += ['-v', '0'] execute(cmd, capture_output=False)
if os.path.isfile(path): cached_args.append(arg) continue if arg not in manifest: raise CommandError("Sandbox '%s' not available (not in " "Manifest)" % (arg, )) urls.append(urlparse.urljoin(manifest_url, basename)) filetracker = get_client() download_dir = options['download_dir'] if not os.path.exists(download_dir): os.makedirs(download_dir) try: execute([options['wget'], '--version']) except ExecuteError: raise CommandError("Wget not working. Please specify a working " "Wget binary using --wget option.") if len(urls) > 0: print >> self.stdout, "--- Downloading sandboxes ..." quiet_flag = ['-nv'] if options['quiet'] else [] execute([options['wget'], '-N', '-i', '-'] + quiet_flag, stdin='\n'.join(urls), capture_output=False, cwd=download_dir) print >> self.stdout, "--- Saving sandboxes to the Filetracker ..." for arg in args:
def test_cat_with_string_input(self): input_text = 'hello there!' output_text = execute('cat', stdin=input_text) self.assertEqual(input_text, output_text)
def test_ignored_error(self): execute('exit 12', ignore_errors=True)
def test_ignored_error_list(self): execute('exit 12', errors_to_ignore=(12, 15, 16)) execute('exit 15', errors_to_ignore=(12, 15, 16)) with self.assertRaises(ExecuteError): execute('return 14')
def test_line_splitting(self): self.assertListEqual(['foo', 'lol'], execute('echo "foo\nlol"', split_lines=True))
def test_cwd(self): self.assertEqual(execute(['cat', os.path.basename(__file__)], cwd=os.path.dirname(__file__)), open(__file__, 'rb').read())
def test_env(self): self.assertEqual('bar\n', execute('echo $foo', env={'foo': 'bar'}))
def generate_all(dir, verbose): generate_from_template( dir, "settings.py", { "__CONFIG_VERSION__": str(INSTALLATION_CONFIG_VERSION), "__DIR__": dir, "__SECRET__": str(uuid.uuid4()), "__TIMEZONE__": get_timezone(), }, ) settings = {} settings_py = os.path.join(dir, "settings.py") execfile(settings_py, globals(), settings) media_root = settings["MEDIA_ROOT"] os.mkdir(media_root) static_root = settings["STATIC_ROOT"] os.mkdir(static_root) os.mkdir(os.path.join(dir, "logs")) os.mkdir(os.path.join(dir, "pidfiles")) virtual_env = os.environ.get("VIRTUAL_ENV", "") user = pwd.getpwuid(os.getuid())[0] manage_py = os.path.join(dir, "manage.py") generate_from_template( dir, "manage.py", {"__DIR__": dir, "__PYTHON_EXECUTABLE__": sys.executable, "__VIRTUAL_ENV__": virtual_env}, mode=0755, ) generate_from_template(dir, "supervisord.conf", {"__USER__": user}) generate_from_template(dir, "wsgi.py", {"__DIR__": dir, "__VIRTUAL_ENV__": virtual_env}) generate_from_template(dir, "start_supervisor.sh", {"__DIR__": dir, "__VIRTUAL_ENV__": virtual_env}, mode=0755) generate_from_template( dir, "apache-site.conf", {"__DIR__": dir, "__STATIC_URL__": settings["STATIC_URL"], "__STATIC_ROOT__": settings["STATIC_ROOT"]}, ) generate_from_template( dir, "nginx-site.conf", {"__DIR__": dir, "__STATIC_URL__": settings["STATIC_URL"], "__STATIC_ROOT__": settings["STATIC_ROOT"]}, ) # Having DJANGO_SETTINGS_MODULE here would probably cause collectstatic to # run with wrong settings. os.environ.pop("DJANGO_SETTINGS_MODULE", None) # Let's silence collectstatic a bit - a ton of (normally) useless logs # happen from it print >>sys.stderr, "Collecting static files..." cmd = [sys.executable, manage_py, "collectstatic", "--noinput"] if not verbose: cmd += ["-v", "0"] execute(cmd, capture_output=False)
print >>self.stdout, "--- Preparing ..." urls = [] for arg in args: if arg not in manifest: raise CommandError("Sandbox '%s' not available (not in " "Manifest)" % (arg,)) urls.append(urlparse.urljoin(manifest_url, arg + '.tar.gz')) filetracker = get_client() dir = options['download_dir'] if not os.path.exists(dir): os.makedirs(dir) try: execute([options['wget'], '--version']) except ExecuteError: raise CommandError("Wget not working. Please specify a working " "Wget binary using --wget option.") print >>self.stdout, "--- Downloading sandboxes ..." execute([options['wget'], '-N', '-i', '-'], stdin='\n'.join(urls), capture_output=False, cwd=dir) print >>self.stdout, "--- Saving sandboxes to the Filetracker ..." for arg in args: basename = arg + '.tar.gz' local_file = os.path.join(dir, basename) print >>self.stdout, " ", basename filetracker.put_file('/sandboxes/' + basename, local_file) os.unlink(local_file)