Source code for libka.pkgedit.ka_src_pkg_git_repo

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# pylint: disable=line-too-long
# kate: space-indent on; indent-width 4; replace-tabs on; indent-mode python; remove-trailing-space modified;
# vim: expandtab ts=4
# pylint: enable=line-too-long

############################################################################
#   Copyright © 2018 José Manuel Santamaría Lema <panfaust@gmail.com>      #
#                                                                          #
#   This program is free software; you can redistribute it and/or modify   #
#   it under the terms of the GNU General Public License as published by   #
#   the Free Software Foundation; either version 2 of the License, or      #
#   (at your option) any later version.                                    #
############################################################################

"""This module just provides the KASrcPkgGitRepo class"""

import os

from libka.pkgedit.ka_src_pkg import KASrcPkg
from libka.pkgedit.ka_git_repo import KAGitRepo


[docs] class KASrcPkgGitRepo(KAGitRepo, KASrcPkg): """This class is meant to represent git repositories where our source packages are mainatained and allow the programmer to extract information about the package, make changes in that source package and perform git operations""" def __init__(self): #Check if we are in a git repository if not os.path.isdir('./.git/'): raise Exception("Git repository not found here") #Call super constructors KAGitRepo.__init__(self, '.') KASrcPkg.__init__(self) self._auto_commit = False self._noci = False self._commit_message = None
[docs] def set_auto_commit(self, auto_commit, noci, commit_message=None): """ If `auto_commit` is `True` it will commit the changes to git if that's appropiate. If `noci` is `True` it will include the 'NOCI' in the commit message to avoid triggering a KCI rebuild with the change. If `commit_message` is `None` it will use a pre-defined commit message. """ self._auto_commit = auto_commit self._noci = noci self._commit_message = commit_message
[docs] def fix_rules_perms(self): """Calling this function will set debian/rules permissions to 0755, and, if no_commit is True, it will commit the changes to git (if any). Returns False if the permissions are already 0755, otherwise returns True.""" #Fix rules permissions result = KASrcPkg.fix_rules_perms(self) #Commit the changes if it's appropiate if result and self._auto_commit: if self._commit_message is None: commit_message = "Fix debian/rules permissions" else: commit_message = self._commit_message if self._noci: commit_message = "NOCI\n" + commit_message self.index.add(['debian/rules']) self.index.commit(commit_message) #Return value return result
[docs] def sanitize_control_file(self, file_name='debian/control'): """ Sanitizes a debian/control file so it won't trigger bugs from python-debian or wrap-and-sort. """ #Sanitize control file KASrcPkg.sanitize_control_file(self) #Commit the changes if it's appropiate if self._auto_commit: #Check if the control file was modified, if it wasn't we must not commit if file_name in self.get_pkg_modified_files(): #Compose the commit message if self._commit_message is None: commit_message = "Sanitize %s file" % file_name else: commit_message = self._commit_message if self._noci: commit_message = "NOCI\n" + commit_message #Add the control file and commit self.index.add([file_name]) self.index.commit(commit_message)
[docs] def new_upstream_release(self, dist, new_upstream_version=None, bump_autopkgtest=False): """ Makes all the needed changes in the packaging to provide a new upstream release. If the new upstream release was already done, returns False and does nothing, otherwise returns the new upstream version. """ new_upstream_version = KASrcPkg.new_upstream_release(self, dist, new_upstream_version) if self._auto_commit and new_upstream_version: if self._commit_message is None: stability = self.get_stability(new_upstream_version) if stability is None: commit_message = "New upstream release (%s)" % new_upstream_version else: commit_message = ("New upstream (%s) release (%s)" % (stability, new_upstream_version)) else: commit_message = self._commit_message if self._noci: commit_message = "NOCI\n" + commit_message if bump_autopkgtest: self.index.add(['debian/changelog', 'debian/control', 'debian/tests/control']) else: self.index.add(['debian/changelog', 'debian/control']) self.index.commit(commit_message) return new_upstream_version
[docs] def set_kubuntu_maintainer_fields(self): """ This method updates the `Maintainer` and `XSBC-Original-Maintainer` fields for Kubuntu if needed. """ changes_done = KASrcPkg.set_kubuntu_maintainer_fields(self) #Commit the changes if it's appropiate #Check if the control file was modified, if it wasn't we must not commit file_name = 'debian/control' if self._auto_commit and changes_done: #Compose the commit message if self._commit_message is None: commit_message = "Set Kubuntu maintainer fields" else: commit_message = self._commit_message if self._noci: commit_message = "NOCI\n" + commit_message #Add the control file and commit self.index.add([file_name]) self.index.commit(commit_message)
[docs] def set_kubuntu_vcs_fields(self): """ This method updates the `Vcs-Browser` and `Vcs-Git` fields for Kubuntu if needed. """ changes_done = KASrcPkg.set_kubuntu_vcs_fields(self) #Commit the changes if it's appropiate #Check if the control file was modified, if it wasn't we must not commit file_name = 'debian/control' if self._auto_commit and changes_done: #Compose the commit message if self._commit_message is None: commit_message = "Set Kubuntu vcs fields" else: commit_message = self._commit_message if self._noci: commit_message = "NOCI\n" + commit_message #Add the control file and commit self.index.add([file_name]) self.index.commit(commit_message)
[docs] def get_pkg_untracked_files(self): """ Returns a list of packaging untracked files. I.e. untracked files under the `debian/` directory. """ result = [] for file_name in self.untracked_files: if file_name.startswith("debian/"): result.append(file_name) return result
[docs] def get_upstream_untracked_files(self): """ Returns a list of upstream source code untracked files. """ result = [] for file_name in self.untracked_files: if not file_name.startswith("debian/"): result.append(file_name) return result
[docs] def get_pkg_modified_files(self): """ Returns a list of modified packaging files in the current clone. """ #Initialize result list result = [] # Take note of the modified files diff_lists = [self.index.diff(None), self.index.diff(self.active_branch.commit)] for diff_list in diff_lists: for diff in diff_list: file_name = diff.a_path if file_name.startswith("debian/"): result.append(file_name) #Return result return result
[docs] def get_unmerged_files(self): """ Returns a list of unmerged files in the current clone. """ #Initialize result list result = set() #we use a set becase we might get dupes #Take note of unmerged files for (path, stage), _ in self.index.entries.items(): if stage != 0: result.add(path) #Return result return list(result)
[docs] def unpack_upstream_source(self, force=False): #pylint: disable=arguments-differ """ Unpacks the upstream source code in the source package directory. If `force` is `False` it will raise an exception if the upstream source code was already unpacked. """ if not force and self.get_upstream_untracked_files(): raise Exception("The upstream source code was already unpacked") else: return KASrcPkg.unpack_upstream_source(self)