From 3da5045bbba3b98bfaadd8fef47ac6540388f38d Mon Sep 17 00:00:00 2001 From: "Chakib (spike) Benziane" Date: Tue, 9 Jul 2013 00:14:11 +0200 Subject: [PATCH] initial commit --- README.rst | 26 ++++++++++++++++ setup.py | 57 ++++++++++++++++++++++++++++++++++ tmpbrowse/__init__.py | 1 + tmpbrowse/tmpbrowse.py | 70 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 README.rst create mode 100644 setup.py create mode 100644 tmpbrowse/__init__.py create mode 100755 tmpbrowse/tmpbrowse.py diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..52dbe2d --- /dev/null +++ b/README.rst @@ -0,0 +1,26 @@ +===================================== +tmpbrowse - Spawn test browser sessions +===================================== + +This tool helps you spawn temporary browser sessions (only chrome for instance) +in a sandboxed user dir to securely test your web app without worrying about +clearing cache, extensions ... + +Installation +============ + +Using pypi repository:: + + $> pip install tmpbrowse + +Forking this repo:: + + $> python setup.py install + + +Usage +===== + +:: + + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..e9b4407 --- /dev/null +++ b/setup.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python + +from setuptools import setup, find_packages +import tmpbrowse +import os + + +def read(*names): + values = dict() + extensions = ['.txt', '.rst'] + for name in names: + value = '' + for extension in extensions: + filename = name + extension + if os.path.isfile(filename): + value = open(name + extension).read() + break + values[name] = value + return values + +long_description = """ +%(README)s + +CHANGELOG +========= + +%(CHANGES)s + +""" % read('README', 'CHANGES') + +setup( + name='tmpbrowse', + version=tmpbrowse.__version__, + description='Spawns a temporary browser session for testing with your app development ', + long_description=long_description, + classifiers=[ + "Development Status :: 3 - Alpha", + "Environment :: Console", + "Intended Audience :: Developers", + "Programming Language :: Python :: 2", + "Programming Language :: Python :: 2.7", + "Topic :: Software Development :: Testing", + ], + keywords='tmpbrowse browser chrome session test web development', + author='Chakib Benziane', + author_email='chakib.benz@gmail.com', + maintainer='Chakib Benziane', + maintainer_email='chakib.benz@gmail.com', + url='https://github.com/sp4ke/tmpbrowse', + license='MIT', + packages=find_packages(), + entry_points={ + 'console_scripts': [ + 'tmpbrowse = tmpbrowse.tmpbrowse:cli_run', + ] + }, +) diff --git a/tmpbrowse/__init__.py b/tmpbrowse/__init__.py new file mode 100644 index 0000000..b8023d8 --- /dev/null +++ b/tmpbrowse/__init__.py @@ -0,0 +1 @@ +__version__ = '0.0.1' diff --git a/tmpbrowse/tmpbrowse.py b/tmpbrowse/tmpbrowse.py new file mode 100755 index 0000000..27d565a --- /dev/null +++ b/tmpbrowse/tmpbrowse.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python + + +import argparse +import os +import subprocess +import shutil + + +CHROME_BIN = 'google-chrome' +CHROME_PARAMS = '--user-data-dir=' +USER_DIRS = os.path.join(os.environ['HOME'], '.chrome_dirs') + +class SpawnChrome(object): + def __init__(self, args): + self.my_args = args[0] + self.chrome_args = args[1] + if self.my_args.project_name: + self.project_name = self.my_args.project_name + self.project_path = os.path.join(USER_DIRS, self.project_name) + # check if USER_DIRS exists otherwise make it + if not os.path.exists(USER_DIRS): + os.path.os.mkdir(USER_DIRS) + + def mk_project_dir(self): + if not os.path.exists(self.project_path): + os.path.os.mkdir(self.project_path) + + def spawn_chrome(self): + # build the base chrome command + chrome_args = [CHROME_BIN] + [CHROME_PARAMS + self.project_path] + # add any additional params passed to chrome + chrome_args += self.chrome_args + print chrome_args + subprocess.call(chrome_args) + + def ls_projects(self): + for p in os.listdir(USER_DIRS): + print('* %s' % p) + + def rm_project(self, project): + shutil.rmtree(os.path.join(USER_DIRS, project)) + + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description='Spawn a sandboxed chrome instance per project') + parser.add_argument('-p', '--project-name', + help='Project name to spawn a chrome instance for ') + parser.add_argument('-l', help='List existing chrome project dirs', + action='store_true') + parser.add_argument('-d', '--remove-project', + help='Removes the corresponding project dir from chrome dirs', + nargs=1) + args = parser.parse_known_args() + + if args[0].project_name: + spawn = SpawnChrome(args) + spawn.mk_project_dir() + spawn.spawn_chrome() + + if args[0].l: + spawn = SpawnChrome(args) + spawn.ls_projects() + + if args[0].remove_project: + spawn = SpawnChrome(args) + spawn.rm_project(args[0].remove_project[0]) +