gns-deb-diff

automate the documentation of the differences between gNewSense and Debian.
git clone git://git.ricketyspace.net/gns-deb-diff.git
Log | Files | Refs

commit def5342df23cb034db1f9e1c21ce2401b198dcdf
parent 5428e3ac379727e1a4d1c279b940403e5d7898b0
Author: rsiddharth <s@ricketyspace.net>
Date:   Sun, 13 Nov 2016 21:10:36 +0000

Re-arrange functions.

* gd_diff.py (save_gns_readme, slurp_gns_readme, slurp_all_gns_readmes)
(readme_gns_readme, slurp_fields_from_readme): Re-arrange functions.
* tests/test_gd_diff.py (test_config_dir, test_config_file)
(test_read_config_file_fail, test_read_config_file_success)
(test_pkgs_dir, test_mk_pkgs_list, test_readmes_dir, test_wiki_page_dir)
(test_write_wiki_page, test_configured_p_no, test_configured_p_yes)
(test_configure): Re-arrange test methods.

Diffstat:
gd_diff.py | 196++++++++++++++++++++++++++++++++++++++++---------------------------------------
tests/test_gd_diff.py | 286++++++++++++++++++++++++++++++++++++++++----------------------------------------
2 files changed, 243 insertions(+), 239 deletions(-)

diff --git a/gd_diff.py b/gd_diff.py @@ -6,6 +6,7 @@ # gns-deb-diff is under the Public Domain. See # <https://creativecommons.org/publicdomain/zero/1.0> +import argparse import json import os import re @@ -19,6 +20,8 @@ from subprocess import run, PIPE from bs4 import BeautifulSoup +_version = '0.1.0dev0' + # list of recognized fields. field_list = [ "Change-Type", @@ -114,102 +117,6 @@ def get_packages(release): return pkgs -def save_gns_readme(content, release, pkg): - """Save README.gNewsense locally. - - :param str content: - Content of the README.gNewsense file. - :param str release: - Release name. - :param str pkg: - Package name. - """ - # create gns_readme dir. for pkg. - gns_readme_dir = path.join(readmes_dir(release), pkg, 'debian') - - try: - os.makedirs(gns_readme_dir, exist_ok=True) - except Exception as e: - print("Error creating directory '%s'\n Error Info:\n %r" % - (gns_readme_dir, e), file=sys.stderr) - sys.exit(1) - - gns_readme = path.join(gns_readme_dir, 'README.gNewSense') - write_file(gns_readme, content) - print('Saved {}'.format(gns_readme)) - - -def slurp_gns_readme(release, pkg): - """Read and save the README.gNewSense for `pkg` in `release`. - - """ - readme_url = readme_url_fmt.format(release, pkg) - cmd = 'bzr cat {}'.format(readme_url) - cp = execute(cmd, out=PIPE, err=PIPE) - - if(cp.returncode == 0): - save_gns_readme(cp.stdout.decode(), release, pkg) - return True - else: - print("README.gNewSense not found for package {}".format(pkg), - file=sys.stderr) - return False - - -def slurp_all_gns_readmes(release, pkgs): - """Read and save all README.gNewSense for `pkgs` in `release`. - - Returns list of packages in `pkgs` that does not have README.gNewSense. - """ - pkgs_noreadmes = [] - for pkg in pkgs: - slurped = slurp_gns_readme(release, pkg) - - if(not slurped): - pkgs_noreadmes.append(pkg) - - return pkgs_noreadmes - - -def read_gns_readme(release, pkg): - """Returns content of README.gNewSense for `pkg`. - - If `README.gNewSense` does not exists for `pkg`, None is returned. - - """ - readme_path = path.join(readmes_dir(release), pkg, 'debian', - 'README.gNewSense') - - if not path.isfile(readme_path): - return None - - readme_content = read_file(readme_path) - return readme_content - - -def slurp_fields_from_readme(content): - """Returns dict containing fields slurped from `content` - - - If a field is not defined or if its value is empty in the - `content`, then its corresponding value in the dict will be None. - - """ - field_values = {} - for field in field_list: - pattern = r'{}:[ ]*(.+)'.format(field) - field_pattern = re.compile(pattern) - field_match = field_pattern.search(content) - - if (field_match and - field_match.group(1) and - field_match.group(1).strip()): - field_values[field] = field_match.group(1).strip() - else: - field_values[field] = None - - return field_values - - def config_dir(): """Return the gns-deb-diff config directory. @@ -265,6 +172,7 @@ def mk_pkgs_list(release): return pkgs_file + def readmes_dir(release): """Return readmes directory for `release`. @@ -324,3 +232,99 @@ def configure(): json.dump(config, open(config_file(), 'w')) os.chmod(config_file(), mode=0o600) + + +def save_gns_readme(content, release, pkg): + """Save README.gNewsense locally. + + :param str content: + Content of the README.gNewsense file. + :param str release: + Release name. + :param str pkg: + Package name. + """ + # create gns_readme dir. for pkg. + gns_readme_dir = path.join(readmes_dir(release), pkg, 'debian') + + try: + os.makedirs(gns_readme_dir, exist_ok=True) + except Exception as e: + print("Error creating directory '%s'\n Error Info:\n %r" % + (gns_readme_dir, e), file=sys.stderr) + sys.exit(1) + + gns_readme = path.join(gns_readme_dir, 'README.gNewSense') + write_file(gns_readme, content) + print('Saved {}'.format(gns_readme)) + + +def slurp_gns_readme(release, pkg): + """Read and save the README.gNewSense for `pkg` in `release`. + + """ + readme_url = readme_url_fmt.format(release, pkg) + cmd = 'bzr cat {}'.format(readme_url) + cp = execute(cmd, out=PIPE, err=PIPE) + + if(cp.returncode == 0): + save_gns_readme(cp.stdout.decode(), release, pkg) + return True + else: + print("README.gNewSense not found for package {}".format(pkg), + file=sys.stderr) + return False + + +def slurp_all_gns_readmes(release, pkgs): + """Read and save all README.gNewSense for `pkgs` in `release`. + + Returns list of packages in `pkgs` that does not have README.gNewSense. + """ + pkgs_noreadmes = [] + for pkg in pkgs: + slurped = slurp_gns_readme(release, pkg) + + if(not slurped): + pkgs_noreadmes.append(pkg) + + return pkgs_noreadmes + + +def read_gns_readme(release, pkg): + """Returns content of README.gNewSense for `pkg`. + + If `README.gNewSense` does not exists for `pkg`, None is returned. + + """ + readme_path = path.join(readmes_dir(release), pkg, 'debian', + 'README.gNewSense') + + if not path.isfile(readme_path): + return None + + readme_content = read_file(readme_path) + return readme_content + + +def slurp_fields_from_readme(content): + """Returns dict containing fields slurped from `content` + + - If a field is not defined or if its value is empty in the + `content`, then its corresponding value in the dict will be None. + + """ + field_values = {} + for field in field_list: + pattern = r'{}:[ ]*(.+)'.format(field) + field_pattern = re.compile(pattern) + field_match = field_pattern.search(content) + + if (field_match and + field_match.group(1) and + field_match.group(1).strip()): + field_values[field] = field_match.group(1).strip() + else: + field_values[field] = None + + return field_values diff --git a/tests/test_gd_diff.py b/tests/test_gd_diff.py @@ -103,6 +103,149 @@ class TestGdDiff(object): assert_equal(pkgs, 'antlr\napt\napt-setup\nautoconf\nautoconf2.59\nautoconf2.64\nbacula\nbase-files\nbase-installer\nbatik\ncairomm\ncdebootstrap\ncfitsio3\nchoose-mirror\nclaws-mail\ndb4.6\ndb4.7\ndb4.8\ndebian-cd\ndebian-edu\ndebian-installer\ndebian-installer-launcher\ndebootstrap\ndesktop-base\ndoc-linux\ndoc-linux-hr\ndoc-linux-it\ndoc-linux-ja\ndoc-linux-pl\nenscript\nepiphany-browser\nfop\nfreetype\ngalaxia\ngdm3\nglibmm2.4\ngnewsense-archive-keyring\ngnome-desktop\ngtkmm2.4\nicedove\niceweasel\nkde4libs\nkdebase\nkdebase-workspace\nkdenetwork\nkernel-wedge\nlensfun\nliferea\nlintian\nlinux-2.6\nlinux-kernel-di-amd64-2.6\nlinux-kernel-di-i386-2.6\nlinux-latest-2.6\nlive-build\nlive-config\nmeta-gnome2\nmplayer\nnet-retriever\nobjcryst-fox\nopenbox-themes\nopenjdk-6\nopenoffice.org\npangomm\nperl-tk\npkgsel\npopularity-contest\npsutils\npython-apt\nscreenlets\nsip4-qt3\nsoftware-center\ntcl8.4\ntcl8.5\ntexlive-extra\ntk8.4\ntk8.5\nupdate-manager\nvim\nwmaker\nxchat\nxdm\nxorg-server\nxserver-xorg-video-siliconmotion\nyeeloong-base\n') + def test_config_dir(self): + def env(e): + return self.test_home + + with mock.patch('os.getenv', new=env): + c_dir = config_dir() + assert_equal(c_dir, os.path.join(self.test_home, + '.config', 'gns-deb-diff')) + assert_equal(os.path.isdir(c_dir), True) + + + def test_config_file(self): + def env(e): + return self.test_home + + with mock.patch('os.getenv', new=env): + c_file = config_file() + assert_equal(c_file, os.path.join(self.test_home, '.config', + 'gns-deb-diff', 'config')) + + + def test_read_config_file_fail(self): + config = read_config_file() + assert_equal(config, False) + + + def test_read_config_file_success(self): + def env(e): + return self.test_home + + with mock.patch('os.getenv', new=env): + c_file = config_file() + + # first write sample config file. + json.dump({'user': 'usrnm', 'pass': 'weasaspeciesrfckd'}, + open(c_file, 'w')) + + # now on to the test. + config = read_config_file() + assert_equal(config['user'], 'usrnm') + assert_equal(config['pass'], 'weasaspeciesrfckd') + + + def test_pkgs_dir(self): + def env(e): + return self.test_home + + with mock.patch('os.getenv', new=env): + pd = pkgs_dir() + assert_equal(pd, os.path.join(self.test_home, '.config', + 'gns-deb-diff', 'pkgs')) + assert_equal(os.path.isdir(pd), True) + + + def test_mk_pkgs_list(self): + def env(e): + return self.test_home + + with mock.patch('os.getenv', new=env): + pkgs_file = mk_pkgs_list('parkes') + # test + assert_equal(read_file(pkgs_file), 'antlr\napt\napt-setup\nautoconf\nautoconf2.59\nautoconf2.64\nbacula\nbase-files\nbase-installer\nbatik\ncairomm\ncdebootstrap\ncfitsio3\nchoose-mirror\nclaws-mail\ndb4.6\ndb4.7\ndb4.8\ndebian-cd\ndebian-edu\ndebian-installer\ndebian-installer-launcher\ndebootstrap\ndesktop-base\ndoc-linux\ndoc-linux-hr\ndoc-linux-it\ndoc-linux-ja\ndoc-linux-pl\nenscript\nepiphany-browser\nfop\nfreetype\ngalaxia\ngdm3\nglibmm2.4\ngnewsense-archive-keyring\ngnome-desktop\ngtkmm2.4\nicedove\niceweasel\nkde4libs\nkdebase\nkdebase-workspace\nkdenetwork\nkernel-wedge\nlensfun\nliferea\nlintian\nlinux-2.6\nlinux-kernel-di-amd64-2.6\nlinux-kernel-di-i386-2.6\nlinux-latest-2.6\nlive-build\nlive-config\nmeta-gnome2\nmplayer\nnet-retriever\nobjcryst-fox\nopenbox-themes\nopenjdk-6\nopenoffice.org\npangomm\nperl-tk\npkgsel\npopularity-contest\npsutils\npython-apt\nscreenlets\nsip4-qt3\nsoftware-center\ntcl8.4\ntcl8.5\ntexlive-extra\ntk8.4\ntk8.5\nupdate-manager\nvim\nwmaker\nxchat\nxdm\nxorg-server\nxserver-xorg-video-siliconmotion\nyeeloong-base\n') + + + def test_readmes_dir(self): + def env(e): + return self.test_home + + with mock.patch('os.getenv', new=env): + rd_parkes = readmes_dir('parkes') + assert_equal(rd_parkes, os.path.join(self.test_home, '.config', + 'gns-deb-diff', 'readmes', + 'parkes')) + assert_equal(os.path.isdir(rd_parkes), True) + + + def test_wiki_page_dir(self): + def env(e): + return self.test_home + + with mock.patch('os.getenv', new=env): + wd_parkes = wiki_page_dir('parkes') + assert_equal(wd_parkes, os.path.join(self.test_home, '.config', + 'gns-deb-diff', 'wiki-page', + 'parkes')) + assert_equal(os.path.isdir(wd_parkes), True) + + + def test_write_wiki_page(self): + def env(e): + return self.test_home + + with mock.patch('os.getenv', new=env): + release = 'parkes' + write_wiki_page(release, 'wiki content') + wp_file = os.path.join(wiki_page_dir(release), 'last.rev') + assert_equal(read_file(wp_file), 'wiki content') + + + def test_configured_p_no(self): + def env(e): + return self.test_home + + with mock.patch('os.getenv', new=env): + configured = configured_p() + assert_equal(configured, False) + + + def test_configured_p_yes(self): + def env(e): + return self.test_home + + with mock.patch('os.getenv', new=env): + c_path = config_dir() + c_file = config_file() + + open(c_file, 'w').close() + + configured = configured_p() + assert_equal(configured, True) + + + def test_configure(self): + def env(e): + return self.test_home + + inputs = ['usrnm', 'weasaspeciesrfckd'] + def mock_input(p): + return inputs.pop(0) + + with mock.patch('os.getenv', new=env), \ + mock.patch('builtins.input', new=mock_input): + configure() + + # read config file + config = json.load(open(config_file(), 'r')) + + # tests + assert_equal(config['user'], 'usrnm') + assert_equal(config['pass'], 'weasaspeciesrfckd') + assert_equal(oct(os.stat(config_file()).st_mode), '0o100600') + + def test_save_gns_readme(self): def env(e): return self.test_home @@ -251,149 +394,6 @@ class TestGdDiff(object): 'Removed example with non-free files.') - def test_config_dir(self): - def env(e): - return self.test_home - - with mock.patch('os.getenv', new=env): - c_dir = config_dir() - assert_equal(c_dir, os.path.join(self.test_home, - '.config', 'gns-deb-diff')) - assert_equal(os.path.isdir(c_dir), True) - - - def test_config_file(self): - def env(e): - return self.test_home - - with mock.patch('os.getenv', new=env): - c_file = config_file() - assert_equal(c_file, os.path.join(self.test_home, '.config', - 'gns-deb-diff', 'config')) - - - def test_read_config_file_fail(self): - config = read_config_file() - assert_equal(config, False) - - - def test_read_config_file_success(self): - def env(e): - return self.test_home - - with mock.patch('os.getenv', new=env): - c_file = config_file() - - # first write sample config file. - json.dump({'user': 'usrnm', 'pass': 'weasaspeciesrfckd'}, - open(c_file, 'w')) - - # now on to the test. - config = read_config_file() - assert_equal(config['user'], 'usrnm') - assert_equal(config['pass'], 'weasaspeciesrfckd') - - - def test_pkgs_dir(self): - def env(e): - return self.test_home - - with mock.patch('os.getenv', new=env): - pd = pkgs_dir() - assert_equal(pd, os.path.join(self.test_home, '.config', - 'gns-deb-diff', 'pkgs')) - assert_equal(os.path.isdir(pd), True) - - - def test_mk_pkgs_list(self): - def env(e): - return self.test_home - - with mock.patch('os.getenv', new=env): - pkgs_file = mk_pkgs_list('parkes') - # test - assert_equal(read_file(pkgs_file), 'antlr\napt\napt-setup\nautoconf\nautoconf2.59\nautoconf2.64\nbacula\nbase-files\nbase-installer\nbatik\ncairomm\ncdebootstrap\ncfitsio3\nchoose-mirror\nclaws-mail\ndb4.6\ndb4.7\ndb4.8\ndebian-cd\ndebian-edu\ndebian-installer\ndebian-installer-launcher\ndebootstrap\ndesktop-base\ndoc-linux\ndoc-linux-hr\ndoc-linux-it\ndoc-linux-ja\ndoc-linux-pl\nenscript\nepiphany-browser\nfop\nfreetype\ngalaxia\ngdm3\nglibmm2.4\ngnewsense-archive-keyring\ngnome-desktop\ngtkmm2.4\nicedove\niceweasel\nkde4libs\nkdebase\nkdebase-workspace\nkdenetwork\nkernel-wedge\nlensfun\nliferea\nlintian\nlinux-2.6\nlinux-kernel-di-amd64-2.6\nlinux-kernel-di-i386-2.6\nlinux-latest-2.6\nlive-build\nlive-config\nmeta-gnome2\nmplayer\nnet-retriever\nobjcryst-fox\nopenbox-themes\nopenjdk-6\nopenoffice.org\npangomm\nperl-tk\npkgsel\npopularity-contest\npsutils\npython-apt\nscreenlets\nsip4-qt3\nsoftware-center\ntcl8.4\ntcl8.5\ntexlive-extra\ntk8.4\ntk8.5\nupdate-manager\nvim\nwmaker\nxchat\nxdm\nxorg-server\nxserver-xorg-video-siliconmotion\nyeeloong-base\n') - - - def test_readmes_dir(self): - def env(e): - return self.test_home - - with mock.patch('os.getenv', new=env): - rd_parkes = readmes_dir('parkes') - assert_equal(rd_parkes, os.path.join(self.test_home, '.config', - 'gns-deb-diff', 'readmes', - 'parkes')) - assert_equal(os.path.isdir(rd_parkes), True) - - - def test_wiki_page_dir(self): - def env(e): - return self.test_home - - with mock.patch('os.getenv', new=env): - wd_parkes = wiki_page_dir('parkes') - assert_equal(wd_parkes, os.path.join(self.test_home, '.config', - 'gns-deb-diff', 'wiki-page', - 'parkes')) - assert_equal(os.path.isdir(wd_parkes), True) - - - def test_write_wiki_page(self): - def env(e): - return self.test_home - - with mock.patch('os.getenv', new=env): - release = 'parkes' - write_wiki_page(release, 'wiki content') - wp_file = os.path.join(wiki_page_dir(release), 'last.rev') - assert_equal(read_file(wp_file), 'wiki content') - - - def test_configured_p_no(self): - def env(e): - return self.test_home - - with mock.patch('os.getenv', new=env): - configured = configured_p() - assert_equal(configured, False) - - - def test_configured_p_yes(self): - def env(e): - return self.test_home - - with mock.patch('os.getenv', new=env): - c_path = config_dir() - c_file = config_file() - - open(c_file, 'w').close() - - configured = configured_p() - assert_equal(configured, True) - - - def test_configure(self): - def env(e): - return self.test_home - - inputs = ['usrnm', 'weasaspeciesrfckd'] - def mock_input(p): - return inputs.pop(0) - - with mock.patch('os.getenv', new=env), \ - mock.patch('builtins.input', new=mock_input): - configure() - - # read config file - config = json.load(open(config_file(), 'r')) - - # tests - assert_equal(config['user'], 'usrnm') - assert_equal(config['pass'], 'weasaspeciesrfckd') - assert_equal(oct(os.stat(config_file()).st_mode), '0o100600') - - def teardown(self): """Teardown method for this class.""" if(path.exists(self.gns_pkgs_dir)):