├── .coveragerc ├── .github ├── dependabot.yml └── workflows │ └── test.yaml ├── .gitignore ├── CHANGES.rst ├── CREDITS.rst ├── LICENSE.GPL ├── MANIFEST.in ├── Makefile ├── README.rst ├── TODO.rst ├── checkoutmanager ├── __init__.py ├── config.py ├── dirinfo.py ├── executors.py ├── runner.py ├── sample.cfg ├── tests │ ├── __init__.py │ ├── config.txt │ ├── conftest.py │ ├── custom_actions.py │ ├── custom_actions.txt │ ├── dirinfo.txt │ ├── sample.txt │ ├── testconfig.cfg │ └── utils.txt └── utils.py ├── pyproject.toml ├── requirements.txt └── setup.py /.coveragerc: -------------------------------------------------------------------------------- 1 | [report] 2 | omit = 3 | */migrations/* 4 | */test* 5 | /Library/* 6 | *buildout/eggs/* 7 | /usr/* 8 | /var/lib/hudson/.buildout/* 9 | /var/lib/jenkins/.buildout/* 10 | eggs/* 11 | local_checkouts/* 12 | parts/* 13 | 14 | ignore_errors = true 15 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | 4 | - package-ecosystem: "github-actions" 5 | directory: "/" 6 | schedule: 7 | # Check for updates to GitHub Actions every week 8 | interval: "weekly" 9 | -------------------------------------------------------------------------------- /.github/workflows/test.yaml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | # Run on pull requests and on the master branch itself. 4 | on: 5 | push: 6 | branches: 7 | - master 8 | pull_request: 9 | 10 | 11 | jobs: 12 | build_and_test: 13 | runs-on: ubuntu-latest 14 | strategy: 15 | matrix: 16 | python-version: ['3.9', '3.13'] 17 | steps: 18 | - uses: actions/checkout@v4 19 | - name: Set up Python ${{ matrix.python-version }} 20 | uses: actions/setup-python@v5 21 | with: 22 | python-version: ${{ matrix.python-version }} 23 | - name: Install dependencies 24 | run: make install 25 | - name: Check syntax (ruff) 26 | run: make check && make beautiful 27 | - name: Test (flake8...) 28 | run: make test 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.pyo 3 | .Python 4 | .installed.cfg 5 | bin 6 | coverage 7 | develop-eggs 8 | dist 9 | eggs 10 | include 11 | lib 12 | parts 13 | checkoutmanager.egg-info 14 | .coverage 15 | htmlcov 16 | doc/build/ 17 | /venv/ 18 | -------------------------------------------------------------------------------- /CHANGES.rst: -------------------------------------------------------------------------------- 1 | Changelog of checkoutmanager 2 | ============================ 3 | 4 | 3.2 (unreleased) 5 | ---------------- 6 | 7 | - Nothing changed yet. 8 | 9 | 10 | 3.1 (2025-01-30) 11 | ---------------- 12 | 13 | - Fixed regex strings by making them 'raw' strings. A modern python would otherwise 14 | complain (rightfully so) about invalid escape sequences (``\s``, ``\d``). 15 | 16 | - Internal project change: ``.venv/`` instead of ``venv/``. And removed buildout config 17 | as we've been using virtualenv via the makefile for a while now. 18 | 19 | - Testing on python 3.9 and 3.13 now, instead of ye olde 3.8. 20 | 21 | 22 | 3.0.2 (2024-01-11) 23 | ------------------ 24 | 25 | - Fixed super() call on the MultiExecutor that I messed up when 26 | modernizing the python 2.7 code :-) 27 | 28 | 29 | 3.0.1 (2024-01-11) 30 | ------------------ 31 | 32 | - Cleaned up left-over TODO comments in readme. 33 | 34 | 35 | 3.0 (2024-01-11) 36 | ---------------- 37 | 38 | - Added github action for basic testing. 39 | 40 | - Removed ye olde buildout setup, including z3c.testsetup. Using 41 | pytest now. 42 | 43 | - Removed python 2.7 support, we're on 3.8+ now. 44 | 45 | - Achieved compatibility with python 3.12: 46 | Safeconfigparser->ConfigParser. 47 | 48 | 49 | 2.7 (2021-09-28) 50 | ---------------- 51 | 52 | - More robust error handling. 53 | [mortenlj] 54 | 55 | 56 | 2.6.1 (2019-09-23) 57 | ------------------ 58 | 59 | - Fixed small, but essential, README error. 60 | 61 | 62 | 2.6 (2019-09-10) 63 | ---------------- 64 | 65 | - Updated the setup (mostly: buildout version pins) so that the project can be 66 | developed/tested again. 67 | 68 | - The ``exists`` and ``co`` command used to check only if a directory 69 | existed. Now it also checks if the dot directory (``.git``, ``.svn``) 70 | exists. This way an empty directory also will get filled with a checkout. 71 | 72 | 73 | 2.5 (2016-11-07) 74 | ---------------- 75 | 76 | - Fix #19: sometimes git remote changes were seen where there were none. 77 | [reinout] 78 | 79 | 80 | 2.4.1 (2015-09-10) 81 | ------------------ 82 | 83 | - Bugfix for the 2.4-introduced ``run_one()`` function. 84 | [chintal] 85 | 86 | 87 | 2.4 (2015-09-09) 88 | ---------------- 89 | 90 | - Added ``in`` command that reports incoming changes (so: the changes you'd 91 | get by running ``checkoutmanager up``). Due to differences between versions 92 | of git/svn/hg/bzr, the reporting might not be entirely accurate. It is 93 | *very* hard to get right. So: please `report an issue 94 | `_ if something is not 95 | quite right. 96 | [chintal] 97 | 98 | - Added better support for using checkoutmanager as a library. Provided you 99 | first load a config file, you can now programmatically run actions on 100 | individual directories or urls. See the source code for the 101 | ``checkoutmanager.runner.run_one()`` function. 102 | [chintal] 103 | 104 | 105 | 2.3 (2015-09-08) 106 | ---------------- 107 | 108 | - Added a preserve_tree option to config files to allow structured 109 | checkouts mirroring the repository tree. 110 | [chintal] 111 | 112 | 113 | 2.2 (2015-08-24) 114 | ---------------- 115 | 116 | - Checkoutmanager now also runs **on python 3**! 117 | [reinout] 118 | 119 | - Moved from bitbucket (https://bitbucket.org/reinout/checkoutmanager) to 120 | github (https://github.com/reinout/checkoutmanager). 121 | [reinout] 122 | 123 | 124 | 2.1 (2015-08-18) 125 | ---------------- 126 | 127 | - Fixed ``missing`` command: do not swallow the output when 128 | looking for not yet checked out items. Fixes issue #24. 129 | [maurits] 130 | 131 | 132 | 2.0 (2015-03-25) 133 | ---------------- 134 | 135 | - Huge speed increase because commands are now run in parallel instead of 136 | sequentially. Great fix by Morten Lied Johansen. For me, "checkoutmanager 137 | up" now takes 19 seconds instead of 105 seconds! 138 | 139 | 140 | 1.17 (2015-02-06) 141 | ----------------- 142 | 143 | - Added support for custom commands: now you can write an extension for 144 | checkoutmanager so that you can run ``checkoutmanager 145 | your_custom_command``. See the README for documentation. Patch by Rafael 146 | Oliveira. 147 | 148 | 149 | 1.16 (2015-01-02) 150 | ----------------- 151 | 152 | - Added globbing support for ignores. 153 | 154 | 155 | 1.15 (2013-09-27) 156 | ----------------- 157 | 158 | - Handle corner case in determining directory name for a git clone. 159 | 160 | 161 | 1.14 (2013-08-12) 162 | ----------------- 163 | 164 | - Added ``--force-interactive`` to ``svn info`` for svn version 1.8 165 | and higher. This is for the "hidden" ``instancemanager info`` 166 | command that is handy for updating your repositories when you've 167 | switched svn versions. (See the changelog entry for 1.10). Patch by 168 | Maurits. 169 | 170 | 171 | 1.13 (2012-07-20) 172 | ----------------- 173 | 174 | - Not using the sample config file as the test config file anymore. This means 175 | there's a much nicer and more useful sample config file now. 176 | 177 | (Thanks Craig Blaszczyk for his pull request that was the basis for this!) 178 | 179 | 180 | 1.12 (2012-04-14) 181 | ----------------- 182 | 183 | - For bzr, the "out" command uses the exit code instead of the command output 184 | now. This is more reliable and comfortable. Fix by Jendrik Seipp, thanks! 185 | 186 | 187 | 1.11 (2012-03-20) 188 | ----------------- 189 | 190 | - Allow more than one vcs in a directory. This was already possible 191 | before, but now known you no longer need to list all the checkouts 192 | of the competing vcs in the ignore option. Also, items that are 193 | ignored in one section are now also ignored in other sections for 194 | the same directory. 195 | Fixes #11. 196 | [maurits] 197 | 198 | 199 | 1.10 (2012-01-16) 200 | ----------------- 201 | 202 | - Using --mine-only option to ``bzr missing`` to only show our outgoing 203 | changesets when running checkoutmanager's "out" command for bzr. 204 | 205 | - Copying sample .cfg file if it doesn't exist instead of only suggesting the 206 | copy. Fixes #12. 207 | 208 | - Added hidden info command. Should be only useful for subversion if 209 | your svn program is updated and your OS requires you to give svn 210 | access to your stored credentials again, for each repository. 211 | [maurits] 212 | 213 | 214 | 1.9 (2011-11-08) 215 | ---------------- 216 | 217 | - Added ``upgrade`` command that upgrades your subversion checkouts to 218 | the new 1.7 layout of the ``.svn`` directory. 219 | [maurits] 220 | 221 | 222 | 1.8 (2011-10-13) 223 | ---------------- 224 | 225 | - Using ``git push --dry-run`` now to detect not-yet-pushed outgoing changes 226 | with ``checkoutmanager out``. Fixes #9 (reported by Maurits van Rees). 227 | 228 | 229 | 1.7 (2011-10-06) 230 | ---------------- 231 | 232 | - Added --configfile option. Useful when you want to use checkoutmanager to 233 | manage checkouts for something else than your regular development projects. 234 | In practice: I want to use it for an 'sdistmaker' that works with git. 235 | 236 | 237 | 1.6 (2010-12-27) 238 | ---------------- 239 | 240 | - Full fix for #7: checkoutmanager doesn't stop on the first error, but 241 | continues. And it reports all errors afterwards. This helps when just one 242 | of your svn/hg/whatever servers is down: the rest will just keep working. 243 | 244 | - Partial fix for #7: ``svn up`` runs with ``--non-interactive`` now, so 245 | conflict errors errors are reported instead of pretty much silently waiting 246 | for interactive input that will never come. 247 | 248 | 249 | 1.5 (2010-09-14) 250 | ---------------- 251 | 252 | - Using ``except CommandError, e`` instead of ``except CommandError as e`` for 253 | python2.4 compatibility. 254 | 255 | 256 | 1.4 (2010-08-17) 257 | ---------------- 258 | 259 | - Added git support (patch by Robert Kern: thanks!) Fixes issue #6. 260 | 261 | 262 | 1.3 (2010-08-09) 263 | ---------------- 264 | 265 | - Added new "out" action that shows changesets not found in the default push 266 | location of a repository for a dvcs (hg, bzr). The action doesn't make 267 | sense for svn, so it is ignored for svn checkouts. Fixes issue #1. Thanks 268 | Dmitrii Miliaev for this fix! 269 | 270 | 271 | 1.2.1 (2010-08-06) 272 | ------------------ 273 | 274 | - Bugfix: when reporting an error, the os.getcwd method itself would get 275 | printed instead of the *output* of os.getcwd()... 276 | 277 | 278 | 1.2 (2010-08-04) 279 | ---------------- 280 | 281 | - If the config file doesn't exist, just print the config file hints instead 282 | of the generic usage info. 283 | 284 | - Fixed issue #4: the generic 'buildout' name is stripped from the path. 285 | svn://somewhere/customername/buildout/trunk is a common pattern. 286 | 287 | - Added -v option that prints the commands and the directory where you execute 288 | them. Fixes issue #3. 289 | 290 | - Reporting on not yet checked out items when running "checkoutmanager 291 | missing". Fixes issue #2. 292 | 293 | - Checking return code from executed commands. On error, the command and 294 | working directory is printed and also the output. And the script stops 295 | right away. Fixes #5. 296 | 297 | - Updated the documentation, for instance by mentioning the config file name 298 | and location. 299 | 300 | 301 | 1.1 (2010-08-02) 302 | ---------------- 303 | 304 | - Switched from "commands" module to "subprocesses" for windows 305 | compatibility. 306 | 307 | 308 | 1.0 (2010-08-01) 309 | ---------------- 310 | 311 | - Small fixes. It works great in practice. 312 | 313 | - Moved from bzr to hg and made it public on bitbucket.org. 314 | 315 | - Big documentation update as I'm going to release it. 316 | 317 | 318 | 0.1 (2010-05-07) 319 | ---------------- 320 | 321 | - First reasonably working version. 322 | 323 | - Initial library skeleton created by thaskel. 324 | -------------------------------------------------------------------------------- /CREDITS.rst: -------------------------------------------------------------------------------- 1 | Credits 2 | ======= 3 | 4 | - Created by `Reinout van Rees `_. 5 | 6 | - "out" command by Dmitrii Miliaev. 7 | 8 | - Git support by Robert Kern. 9 | 10 | - Globbing support for the ignores by Patrick Gerken. 11 | 12 | - Custom commands support by Rafael Oliveira. 13 | 14 | - Parallelism code by Morten Lied Johansen. 15 | 16 | Source code is on github at https://github.com/reinout/checkoutmanager . 17 | 18 | Bugs and feature requests can be reported at 19 | https://github.com/reinout/checkoutmanager/issues . 20 | -------------------------------------------------------------------------------- /LICENSE.GPL: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | recursive-include checkoutmanager *.txt *.py *.cfg 2 | include * 3 | exclude .installed.cfg 4 | global-exclude *.pyc 5 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | install: .venv .venv/bin/checkoutmanager 2 | 3 | 4 | .venv: 5 | python3 -m venv .venv 6 | .venv/bin/pip install --upgrade pip 7 | 8 | 9 | .venv/bin/checkoutmanager: setup.py requirements.txt 10 | .venv/bin/pip install -r requirements.txt 11 | 12 | 13 | test: 14 | .venv/bin/pytest checkoutmanager --doctest-glob="tests/*.txt" 15 | 16 | 17 | check: 18 | .venv/bin/ruff check checkoutmanager --fix 19 | 20 | 21 | beautiful: 22 | .venv/bin/ruff format checkoutmanager 23 | 24 | 25 | clean: 26 | rm -rf .venv 27 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | Checkoutmanager 2 | =============== 3 | 4 | Makes bzr/hg/git/svn checkouts in several places according to a 5 | ``.checkoutmanager.cfg`` config file (in your home directory). 6 | 7 | The advantage: you've got one command with which you can update all your 8 | checkouts. And with which you can ask for a list of uncommitted changes. And 9 | you can rebuild your entire checkout structure on a new machine just by 10 | copying the config file (this was actually the purpose I build it for: I had 11 | to change laptops when I switched jobs...). 12 | 13 | Checkoutmanager works on linux, osx and windows. 14 | 15 | 16 | Starting to use it 17 | ------------------ 18 | 19 | Starting is easy. Just ``pip install checkoutmanager`` and run 20 | ``checkoutmanager``. 21 | 22 | - The first time, you'll get a sample configuration you can copy to 23 | ``.checkoutmanager.cfg`` in your home directory. 24 | 25 | - The second time, you'll get a usage message. (You'll want to do 26 | ``checkoutmanager co`` to grab your initial checkouts). 27 | 28 | 29 | Generic usage 30 | ------------- 31 | 32 | What I normally do every morning when I get to work is ``checkoutmanager 33 | up``. This grabs the latest versions of all my checkouts from the server(s). 34 | So an ``svn up`` for my subversion checkouts, a ``hg pull -u`` for mercurial 35 | and so on. 36 | 37 | From time to time, I'll do a ``checkoutmanager st`` to show if I've got some 38 | uncommitted files lying around somewhere. Very handy if you've worked in 39 | several directories throughout the day: it prevents you from forgetting to 40 | check in that one bugfix for a whole week. 41 | 42 | A new project means I add a single line to my config file and run 43 | ``checkoutmanager co``. 44 | 45 | Checkoutmanager allows you to spread your checkouts over multiple 46 | directories. It cannot mix version control systems per directory, however. 47 | As an example, I've got a ``~/buildout/`` directory with my big svn website 48 | projects checked out there. And a directory with my svn work python 49 | libraries. And a ``~/hg/`` dir with my mercurial projects. And I've made 50 | checkouts of several config directories in my home dir, such as 51 | ``~/.emacs.d``, ``~/.subversion`` and so on. Works just fine. 52 | 53 | 54 | Commands 55 | -------- 56 | 57 | Available commands: 58 | 59 | exists 60 | Print whether checkouts are present or missing 61 | 62 | up 63 | Grab latest version from the server. 64 | 65 | st 66 | Print status of files in the checkouts 67 | 68 | co 69 | Grab missing checkouts from the server 70 | 71 | missing 72 | Print directories that are missing from the config file 73 | 74 | out 75 | Show changesets you haven't pushed to the server yet 76 | 77 | in 78 | Show incoming changesets that would be pulled in with 'up'. For some 79 | version control systems, this depends on the English output of the 80 | respective commands and is therefore inherently fragile. 81 | 82 | 83 | 84 | Hidden commands 85 | --------------- 86 | 87 | A few commands are hidden because they are seldom used and are only 88 | useful for subversion. 89 | 90 | upgrade 91 | This upgrades the working copy to the new subversion 1.7 layout of 92 | the .svn directory. This should be done once after you have 93 | upgraded your subversion to 1.7. Note that when you accidentally 94 | run this twice you get an error, but nothing breaks. Since this 95 | command is so rarely needed, it is not advertised in the command 96 | line help. 97 | 98 | info 99 | Display the svn info for the remote url. This is useful when your 100 | svn program has been updated and the security mechanisms on your OS 101 | now require you to explictly allow access to the stored credentials. 102 | The other commands either do not access the internet or are 103 | non-interactive (like command up). In fact, the reason for adding 104 | this command is that a non-interactive 'svn update' will fail when 105 | you have not granted access to your credentials yet for this new svn 106 | program. This has happened a bit too often for me (Maurits). 107 | 108 | 109 | Output directory naming 110 | ----------------------- 111 | 112 | If you don't specify an output directory name for your checkout url, it just 113 | takes the last part. To make life easier, we do have some adjustments we 114 | make: 115 | 116 | - ``https://xxx/yyy/product/trunk`` becomes ``product`` instead of 117 | ``trunk``. (Handy for subversion). 118 | 119 | - ``https://xxx/yyy/product/branches/experiment`` becomes 120 | ``product_experiment`` instead of ``experiment`` (Handy for subversion). 121 | 122 | - ``https://xxx/customername/buildout/trunk`` becomes ``customername`` 123 | instead of "trunk" or "buildout". (Old convention we still support). 124 | 125 | - Bzr checkouts that start with "lp:" (special launchpad urls) get their "lp:" 126 | stripped. 127 | 128 | - Git checkouts lose the ".git" at the end of the url. 129 | 130 | - If you want to preserve the directory configuration of your version control 131 | system, add the ``preserve_tree`` option to a group. It should contain one 132 | or more base checkout urls (one per line). If the checkout url starts with 133 | one of the ``preserve_tree`` urls, the folder structure after it is 134 | preserved. 135 | 136 | With a ``preserve_tree`` of ``https://github.com``, 137 | ``https://github.com/reinout/checkoutmanager`` becomes 138 | ``reinout/checkoutmanager`` instead of ``checkoutmanager``. Also handy for 139 | subversion, which often has nested directories. 140 | 141 | If the ``preserve_tree`` base url isn't found, the standard rules are used, 142 | so you won't get an error. 143 | 144 | If you want different behaviour from the defaults above, just specify a 145 | directory name (separated by a space) in the configuration file after the 146 | url. So ``https://github.com/reinout/checkoutmanager bla_bla`` becomes 147 | ``bla_bla`` instead of ``checkoutmanager`` 148 | 149 | 150 | Custom commands 151 | --------------- 152 | 153 | You can write your own custom commands. To do that you need to create a Python 154 | package and register an entry point in your ``setup.py`` for the 155 | ``checkoutmanager.custom_actions`` target. 156 | 157 | A ``test`` command is included with ``checkoutmanager`` and can serve as an 158 | example. It is registered like this in checkoutmanager's own ``setup.py``: 159 | 160 | .. code:: python 161 | 162 | entry_points={ 163 | 'checkoutmanager.custom_actions': [ 164 | 'test = checkoutmanager.tests.custom_actions:test_action' 165 | ] 166 | } 167 | 168 | The entry point function must take one positional argument which will be the 169 | ``checkoutmanager.dirinfo.DirInfo`` instance associated with the directroy 170 | for which the command is being executed. The function can also take optional 171 | keyword arguments. See ``checkoutmanager.tests.custom_actions.test_action`` for 172 | an example. 173 | 174 | Arguments are passed to the custom command using the following syntax: 175 | 176 | .. code:: bash 177 | 178 | checkoutmanager action:arg1=val1,arg2=val2 179 | 180 | 181 | Config file 182 | ----------- 183 | 184 | .. Comment: the config file is included into the long description by setup.py, 185 | it is in checkoutmanager/sample.cfg! 186 | 187 | Sample configuration file:: 188 | -------------------------------------------------------------------------------- /TODO.rst: -------------------------------------------------------------------------------- 1 | TODO 2 | ==== 3 | 4 | - Perhaps make a better sample config (one that actually works instead of the 5 | current one that's structured for benefit of the automated tests). 6 | -------------------------------------------------------------------------------- /checkoutmanager/__init__.py: -------------------------------------------------------------------------------- 1 | # package 2 | -------------------------------------------------------------------------------- /checkoutmanager/config.py: -------------------------------------------------------------------------------- 1 | """Config file parsing and massaging""" 2 | 3 | import configparser 4 | import glob 5 | import os 6 | 7 | from checkoutmanager import dirinfo 8 | 9 | DEFAULTS = { 10 | "report-missing": "true", 11 | "ignore": "", 12 | "preserve_tree": "", 13 | } 14 | 15 | 16 | def linesstring_as_list(string): 17 | """Return \n separated string as a list""" 18 | lines = string.split("\n") 19 | lines = [line.strip() for line in lines] 20 | lines = [line for line in lines if line and not line.startswith("#")] 21 | return lines 22 | 23 | 24 | def extract_spec(spec, preserve_tree=None): 25 | """Extract vcs spec into vcs url and directoryname""" 26 | vcs_url = None 27 | directory = None 28 | parts = spec.split() 29 | assert len(parts) <= 2, spec 30 | if len(parts) == 2: 31 | vcs_url = parts[0] 32 | directory = parts[1] 33 | if len(parts) == 1: 34 | vcs_url = parts[0] 35 | if directory is None: 36 | # preserve_tree provides a list of server_roots. 37 | # If there are no defined server_roots, this effectively 38 | # disables te preserve_tree option and the directoy is 39 | # obtained per usual 40 | server_roots = preserve_tree or [] 41 | for server_root in server_roots: 42 | if vcs_url.startswith(server_root): 43 | directory = vcs_url[len(server_root) :] 44 | break 45 | if directory is None: 46 | parts = [part for part in vcs_url.split("/") if part] 47 | # Common structure: having a customer folder with a 'buildout' 48 | # directory in it. Don't name it 'buildout'. 49 | parts = [part for part in parts if part != "buildout"] 50 | directory = parts[-1] 51 | # Remove /trunk from the end. We don't want that as a name. 52 | if parts[-1] == "trunk": 53 | parts.pop() 54 | directory = parts[-1] 55 | # If we have an svn branch, name it after the project *and* the 56 | # branch. 57 | if (len(parts) > 3) and (parts[-2] == "branches"): 58 | branchname = parts[-1] 59 | projectname = parts[-3] 60 | directory = projectname + "-" + branchname 61 | # Common for bzr projects hosted on launchpad: they're prefixed with 62 | # 'lp:'. Remove that from the name. 63 | if directory.startswith("lp:"): 64 | directory = directory[3:] 65 | if directory.endswith(".git"): 66 | directory = directory[:-4] 67 | if ":" in directory: 68 | # For example git@git.example.org:projectname 69 | directory = directory.split(":")[-1] 70 | return vcs_url, directory 71 | 72 | 73 | class Config: 74 | """Wrapper around config file for returning DirInfo objects""" 75 | 76 | def __init__(self, config_filename): 77 | assert os.path.exists(config_filename) # Just for me atm... 78 | self.config_filename = config_filename 79 | self.parser = configparser.ConfigParser(DEFAULTS) 80 | self.parser.read(config_filename) 81 | 82 | @property 83 | def groupings(self): 84 | return sorted(self.parser.sections()) 85 | 86 | def directories(self, group=None): 87 | """Return wrapped directories""" 88 | result = [] 89 | if group: 90 | sections = [group] 91 | else: 92 | sections = self.groupings 93 | for section in sections: 94 | basedir = self.parser.get(section, "basedir") 95 | vcs = self.parser.get(section, "vcs") 96 | preserve_tree = linesstring_as_list( 97 | self.parser.get(section, "preserve_tree") 98 | ) 99 | dirinfoclass = dirinfo.DirInfo 100 | if vcs == "svn": 101 | dirinfoclass = dirinfo.SvnDirInfo 102 | if vcs == "bzr": 103 | dirinfoclass = dirinfo.BzrDirInfo 104 | if vcs == "hg": 105 | dirinfoclass = dirinfo.HgDirInfo 106 | if vcs == "git": 107 | dirinfoclass = dirinfo.GitDirInfo 108 | checkouts = linesstring_as_list(self.parser.get(section, "checkouts")) 109 | for checkout in checkouts: 110 | url, directory = extract_spec(checkout, preserve_tree) 111 | directory = os.path.join(basedir, directory) 112 | directory = os.path.expanduser(directory) 113 | result.append(dirinfoclass(directory, url)) 114 | return sorted(result) 115 | 116 | def directory_from_url(self, url): 117 | for dir_info in self.directories(): 118 | if dir_info.url == url: 119 | return dir_info 120 | 121 | def directory_from_path(self, abspath, allow_ancestors=True): 122 | for dir_info in self.directories(): 123 | if dir_info.directory == abspath: 124 | return dir_info 125 | if allow_ancestors: 126 | parent = os.path.dirname(abspath) 127 | if parent: 128 | return self.directory_from_path(parent) 129 | 130 | def report_missing(self, group=None): 131 | if group: 132 | sections = [group] 133 | else: 134 | sections = self.groupings 135 | # First get the currently configured items. Note that one 136 | # directory can now contain checkouts from more than one vcs. 137 | base_configured = {} 138 | base_ignored = {} 139 | for section in sections: 140 | checkouts = linesstring_as_list(self.parser.get(section, "checkouts")) 141 | configured = [] 142 | for checkout in checkouts: 143 | url, directory = extract_spec(checkout) 144 | configured.append(directory) 145 | basedir = self.parser.get(section, "basedir") 146 | basedir = os.path.realpath(os.path.expanduser(basedir)) 147 | if basedir not in base_configured: 148 | base_configured[basedir] = [] 149 | base_configured[basedir] += configured 150 | ignore = linesstring_as_list(self.parser.get(section, "ignore")) 151 | if basedir not in base_ignored: 152 | base_ignored[basedir] = [] 153 | base_ignored[basedir] += ignore 154 | 155 | # Now get present and missing items. 156 | for section in sections: 157 | if not self.parser.getboolean(section, "report-missing"): 158 | continue 159 | basedir = self.parser.get(section, "basedir") 160 | basedir = os.path.realpath(os.path.expanduser(basedir)) 161 | present = set(os.listdir(basedir)) 162 | configured = set(base_configured[basedir]) 163 | missing = present - configured 164 | if not missing: 165 | continue 166 | 167 | ignores = base_ignored[basedir] 168 | full_paths_to_ignore = [] 169 | for ignore in ignores: 170 | full_paths_to_ignore += glob.glob(os.path.join(basedir, ignore)) 171 | real_missing = [] 172 | for directory in missing: 173 | full = os.path.join(basedir, directory) 174 | if full in full_paths_to_ignore: 175 | continue 176 | if os.path.isfile(full): 177 | # Files cannot be checkouts, so we ignore 178 | # them. We only deal with directories. 179 | continue 180 | real_missing.append(full) 181 | if real_missing: 182 | print( 183 | "Unconfigured items in %s [%s]:" 184 | % (basedir, self.parser.get(section, "vcs")) 185 | ) 186 | for full in real_missing: 187 | print(" " + full) 188 | -------------------------------------------------------------------------------- /checkoutmanager/dirinfo.py: -------------------------------------------------------------------------------- 1 | """Information on one directory""" 2 | 3 | import os 4 | import re 5 | 6 | from checkoutmanager.utils import CommandError, capture_stdout, system 7 | 8 | # 8-char codes 9 | # '12345678' 10 | CREATED = "created " 11 | MISSING = "missing " 12 | PRESENT = "present " 13 | 14 | 15 | class DirInfo: 16 | """Wrapper for information on one directory""" 17 | 18 | vcs = "xxx" 19 | 20 | def __init__(self, directory, url): 21 | self.directory = directory 22 | self.url = url 23 | 24 | def __repr__(self): 25 | return "" % (self.vcs, self.directory) 26 | 27 | def __lt__(self, other): 28 | # Easy sorting in tests 29 | return self.__repr__() < other.__repr__() 30 | 31 | @property 32 | def parent(self): 33 | return os.path.abspath(os.path.join(self.directory, "..")) 34 | 35 | @property 36 | def exists(self): 37 | expected_dot_dir = os.path.join(self.directory, "." + self.vcs) 38 | return os.path.exists(expected_dot_dir) 39 | 40 | @capture_stdout 41 | def cmd_exists(self, report_only_missing=False): 42 | if self.exists: 43 | answer = PRESENT 44 | if report_only_missing: 45 | return 46 | else: 47 | answer = MISSING 48 | print(" ".join([answer, self.directory])) 49 | 50 | def cmd_rev(self): 51 | raise NotImplementedError() 52 | 53 | def cmd_in(self): 54 | raise NotImplementedError() 55 | 56 | def cmd_up(self): 57 | raise NotImplementedError() 58 | 59 | def cmd_st(self): 60 | raise NotImplementedError() 61 | 62 | def cmd_co(self): 63 | raise NotImplementedError() 64 | 65 | def cmd_out(self): 66 | raise NotImplementedError() 67 | 68 | def cmd_upgrade(self): 69 | # This is only useful for subversion. 70 | pass 71 | 72 | def cmd_info(self): 73 | # This is only known to be useful for subversion. 74 | pass 75 | 76 | 77 | class SvnDirInfo(DirInfo): 78 | vcs = "svn" 79 | 80 | regex_last_changed = re.compile(r"last changed rev: (?P\d+)") 81 | 82 | def _parse_last_changed(self, output): 83 | lines = [line.strip() for line in output.splitlines() if line.strip()] 84 | for line in lines: 85 | m = self.regex_last_changed.match(line.lower()) 86 | if m: 87 | return m.group("rev") 88 | 89 | @capture_stdout 90 | def cmd_rev(self): 91 | print(self.directory) 92 | os.chdir(self.directory) 93 | output = system("svn info") 94 | print(self._parse_last_changed(output)) 95 | 96 | @capture_stdout 97 | def cmd_in(self): 98 | os.chdir(self.directory) 99 | output = system("svn info") 100 | local_rev = self._parse_last_changed(output) 101 | try: 102 | output = system("svn info -r HEAD") 103 | remote_rev = self._parse_last_changed(output) 104 | if remote_rev > local_rev: 105 | print(self.directory) 106 | print(f"Incoming changes : Revision {local_rev} to {remote_rev}") 107 | except CommandError: 108 | print("Could not connect to repository for " + self.directory) 109 | return 110 | 111 | @capture_stdout 112 | def cmd_up(self): 113 | print(self.directory) 114 | os.chdir(self.directory) 115 | print(system("svn up --non-interactive")) 116 | 117 | @capture_stdout 118 | def cmd_st(self): 119 | os.chdir(self.directory) 120 | output = system("svn st --ignore-externals") 121 | lines = [ 122 | line.strip() 123 | for line in output.splitlines() 124 | if line.strip() and not line.startswith("X") 125 | ] 126 | if lines: 127 | print(self.directory) 128 | print(output) 129 | print() 130 | 131 | @capture_stdout 132 | def cmd_co(self): 133 | if not os.path.exists(self.parent): 134 | print("Creating parent dir %s" % self.parent) 135 | os.makedirs(self.parent) 136 | if self.exists: 137 | answer = PRESENT 138 | else: 139 | answer = CREATED 140 | os.chdir(self.parent) 141 | print(system("svn co %s %s" % (self.url, self.directory))) 142 | 143 | print(" ".join([answer, self.directory])) 144 | 145 | @capture_stdout 146 | def cmd_out(self): 147 | # Outgoing changes? We're svn, not some new-fangled dvcs :-) 148 | pass 149 | 150 | @capture_stdout 151 | def cmd_upgrade(self): 152 | # Run 'svn upgrade'. This upgrades the working copy to the 153 | # new subversion 1.7 layout of the .svn directory. 154 | os.chdir(self.directory) 155 | output = system("svn upgrade --quiet") 156 | lines = [ 157 | line.strip() 158 | for line in output.splitlines() 159 | if line.strip() and not line.startswith("X") 160 | ] 161 | print(self.directory) 162 | if lines: 163 | print(output) 164 | print() 165 | 166 | @capture_stdout 167 | def cmd_info(self): 168 | # This is useful when your svn program has been updated and 169 | # the security mechanisms on your OS now require you to 170 | # explictly allow access to the stored credentials. The other 171 | # commands either do not access the internet or are 172 | # non-interactive (like command up). In fact, the reason for 173 | # adding this command is that a non-interactive 'svn update' 174 | # will fail when you have not granted access to your 175 | # credentials yet for this new svn program. This has happened 176 | # a bit too often for me (Maurits). 177 | os.chdir(self.directory) 178 | # Determine the version. 179 | output = system("svn --version --quiet") 180 | try: 181 | version = float(output[:3]) 182 | except (ValueError, TypeError, IndexError): 183 | version = 0.0 184 | # Since version 1.8 we must use --force-interactive, which is 185 | # unavailable in earlier versions. 186 | if version < 1.8: 187 | print(system("svn info %s" % self.url)) 188 | else: 189 | print(system("svn info --force-interactive %s" % self.url)) 190 | 191 | 192 | class BzrDirInfo(DirInfo): 193 | vcs = "bzr" 194 | 195 | @capture_stdout 196 | def cmd_rev(self): 197 | print(self.directory) 198 | os.chdir(self.directory) 199 | print(system("bzr revno")) 200 | 201 | @capture_stdout 202 | def cmd_in(self): 203 | os.chdir(self.directory) 204 | try: 205 | system("bzr missing --theirs-only") 206 | except CommandError as e: 207 | if e.returncode == 1: 208 | # bzr returns 1 if there are incoming changes! 209 | print(self.directory) 210 | print("'bzr missing' reports incoming changesets : ") 211 | print(e.output) 212 | return 213 | if e.returncode == 3: 214 | # bzr returns 3 if there is no parent 215 | pass 216 | else: 217 | raise 218 | return 219 | 220 | @capture_stdout 221 | def cmd_up(self): 222 | print(self.directory) 223 | os.chdir(self.directory) 224 | print(system("bzr up")) 225 | 226 | @capture_stdout 227 | def cmd_st(self): 228 | os.chdir(self.directory) 229 | output = system("bzr st") 230 | if output.strip(): 231 | print(self.directory) 232 | print(output) 233 | print() 234 | 235 | @capture_stdout 236 | def cmd_co(self): 237 | if not os.path.exists(self.parent): 238 | print("Creating parent dir %s" % self.parent) 239 | os.makedirs(self.parent) 240 | if self.exists: 241 | answer = PRESENT 242 | else: 243 | answer = CREATED 244 | os.chdir(self.parent) 245 | print(system("bzr checkout %s %s" % (self.url, self.directory))) 246 | 247 | print(" ".join([answer, self.directory])) 248 | 249 | @capture_stdout 250 | def cmd_out(self): 251 | os.chdir(self.directory) 252 | try: 253 | output = system("bzr missing %s --mine-only" % self.url) 254 | except CommandError as e: 255 | if e.returncode == 1: 256 | # bzr returns 1 if there are outgoing changes! 257 | print("Unpushed outgoing changes in %s:" % self.directory) 258 | print(e.output) 259 | return 260 | else: 261 | raise 262 | print(output) 263 | 264 | 265 | class HgDirInfo(DirInfo): 266 | vcs = "hg" 267 | 268 | regex_changeset = re.compile( 269 | r"changeset:\s+((?P\d+):(?P[0-9a-fA-F]+))" 270 | ) 271 | 272 | @capture_stdout 273 | def cmd_rev(self): 274 | print(self.directory) 275 | os.chdir(self.directory) 276 | output = system("hg log -l1") 277 | lines = [line.strip() for line in output.splitlines() if line.strip()] 278 | for line in lines: 279 | m = self.regex_changeset.match(line.lower()) 280 | if m: 281 | print(f"{m.group('num')}:{m.group('digest')}") 282 | return 283 | 284 | @capture_stdout 285 | def cmd_in(self): 286 | os.chdir(self.directory) 287 | try: 288 | output = system("hg incoming") 289 | print(self.directory) 290 | print("'hg incoming' reports incoming changesets :" % (self.directory)) 291 | print(output) 292 | except CommandError as e: 293 | if e.returncode == 1: 294 | # hg returns 1 if there are no incoming changes. 295 | return 296 | elif e.returncode == 255: 297 | # hg returns 255 if there is no default parent. 298 | pass 299 | else: 300 | raise 301 | return 302 | 303 | @capture_stdout 304 | def cmd_up(self): 305 | print(self.directory) 306 | os.chdir(self.directory) 307 | print(system("hg pull -u %s" % self.url)) 308 | 309 | @capture_stdout 310 | def cmd_st(self): 311 | os.chdir(self.directory) 312 | output = system("hg st") 313 | if output.strip(): 314 | print(self.directory) 315 | print(output) 316 | print() 317 | 318 | @capture_stdout 319 | def cmd_co(self): 320 | if not os.path.exists(self.parent): 321 | print("Creating parent dir %s" % self.parent) 322 | os.makedirs(self.parent) 323 | if self.exists: 324 | answer = PRESENT 325 | else: 326 | answer = CREATED 327 | os.chdir(self.parent) 328 | # TODO: check! 329 | print(system("hg clone %s %s" % (self.url, self.directory))) 330 | 331 | print(" ".join([answer, self.directory])) 332 | 333 | @capture_stdout 334 | def cmd_out(self): 335 | os.chdir(self.directory) 336 | try: 337 | output = system("hg out %s" % self.url) 338 | except CommandError as e: 339 | if e.returncode == 1: 340 | # hg returns 1 if there are no outgoing changes! 341 | # Checkoutmanager is as quiet as possible, so we print 342 | # nothing. 343 | return 344 | else: 345 | raise 346 | # No errors means we have genuine outgoing changes. 347 | print("Unpushed outgoing changes in %s:" % self.directory) 348 | print(output) 349 | 350 | 351 | class GitDirInfo(DirInfo): 352 | vcs = "git" 353 | 354 | regex_commit_digest = re.compile("commit (?P[0-9a-fA-F]+)") 355 | 356 | @capture_stdout 357 | def cmd_rev(self): 358 | print(self.directory) 359 | os.chdir(self.directory) 360 | output = system("git show -q") 361 | lines = [line.strip() for line in output.splitlines() if line.strip()] 362 | for line in lines: 363 | m = self.regex_commit_digest.match(line.lower()) 364 | if m: 365 | print(m.group("object")) 366 | return 367 | 368 | @capture_stdout 369 | def cmd_in(self): 370 | output = system("git pull --dry-run") 371 | output = output.strip() 372 | output_lines = output.split("\n") 373 | if output and len(output_lines): 374 | print( 375 | "'git pull --dry-run' reports possible actions in %s:" 376 | % (self.directory) 377 | ) 378 | print(output) 379 | 380 | @capture_stdout 381 | def cmd_up(self): 382 | print(self.directory) 383 | os.chdir(self.directory) 384 | print(system("git pull")) 385 | 386 | @capture_stdout 387 | def cmd_st(self): 388 | os.chdir(self.directory) 389 | output = system("git status --short") 390 | if output.strip(): 391 | print(self.directory) 392 | print(output) 393 | print() 394 | 395 | @capture_stdout 396 | def cmd_co(self): 397 | if not os.path.exists(self.parent): 398 | print("Creating parent dir %s" % self.parent) 399 | os.makedirs(self.parent) 400 | if self.exists: 401 | answer = PRESENT 402 | else: 403 | answer = CREATED 404 | os.chdir(self.parent) 405 | # TODO: check! 406 | print(system("git clone %s %s" % (self.url, self.directory))) 407 | 408 | print(" ".join([answer, self.directory])) 409 | 410 | @capture_stdout 411 | def cmd_out(self): 412 | os.chdir(self.directory) 413 | output = system("git push --dry-run") 414 | output = output.strip() 415 | output_lines = output.split("\n") 416 | if len(output_lines) > 1: 417 | # More than the 'everything up-to-date' one-liner. 418 | print( 419 | "'git push --dry-run' reports possible actions in %s:" 420 | % (self.directory) 421 | ) 422 | print(output) 423 | -------------------------------------------------------------------------------- /checkoutmanager/executors.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import time 4 | from multiprocessing.pool import Pool 5 | 6 | from checkoutmanager import utils 7 | 8 | 9 | def get_executor(single): 10 | """Return a suitable executor, based on the given flag""" 11 | if single: 12 | return _SingleExecutor() 13 | else: 14 | return _MultiExecutor() 15 | 16 | 17 | class _Executor: 18 | def __init__(self): 19 | self.errors = [] 20 | 21 | def _collector(self, result): 22 | """Collect a result. 23 | 24 | If the result is a CommandError, save it for later, and print it's 25 | message. else, just print the result directly. 26 | 27 | """ 28 | if isinstance(result, utils.CommandError): 29 | self.errors.append(result) 30 | result = result.format_msg() 31 | if not result: 32 | # Don't print empty lines 33 | return 34 | print(result) 35 | 36 | def _error(self, exception): 37 | self.errors.append(exception) 38 | utils.print_exception(exception) 39 | 40 | def execute(self, func, args): 41 | """Execute the given function""" 42 | raise NotImplementedError("Sub-classes must implement this") 43 | 44 | def wait_for_results(self): 45 | """Make sure all results have been collected""" 46 | pass 47 | 48 | 49 | class _SingleExecutor(_Executor): 50 | """Execute functions in the same thread and process (sync)""" 51 | 52 | def execute(self, func, args): 53 | try: 54 | self._collector(func(*args)) 55 | except Exception as e: 56 | self._error(e) 57 | 58 | 59 | class _MultiExecutor(_Executor): 60 | """Execute functions async in a process pool""" 61 | 62 | def __init__(self): 63 | super().__init__() 64 | self._async_results = [] 65 | self.pool = Pool() 66 | 67 | def execute(self, func, args): 68 | self._async_results.append( 69 | self.pool.apply_async( 70 | func, args, callback=self._collector, error_callback=self._error 71 | ) 72 | ) 73 | 74 | def wait_for_results(self): 75 | self.pool.close() 76 | # One would have hoped joining the pool would take care of this, but 77 | # apparently you need to first make sure that all your launched tasks 78 | # has returned their results properly, before calling join, or you 79 | # risk a deadlock. 80 | while any(not r.ready() for r in self._async_results): 81 | time.sleep(0.001) 82 | self.pool.join() 83 | -------------------------------------------------------------------------------- /checkoutmanager/runner.py: -------------------------------------------------------------------------------- 1 | import os 2 | import shutil 3 | import sys 4 | from functools import partial 5 | from optparse import OptionParser 6 | 7 | import pkg_resources 8 | 9 | from checkoutmanager import config, utils 10 | from checkoutmanager.dirinfo import DirInfo 11 | from checkoutmanager.executors import get_executor 12 | 13 | ACTIONS = ["exists", "up", "st", "co", "missing", "out", "in", "rev"] 14 | CONFIGFILE_NAME = "~/.checkoutmanager.cfg" 15 | ACTION_EXPLANATION = { 16 | "exists": "Print whether checkouts are present or missing", 17 | "up": "Grab latest version from the server.", 18 | "st": "Print status of files in the checkouts", 19 | "co": "Grab missing checkouts from the server", 20 | "missing": "Print directories that are missing from the config file", 21 | "out": "Show changesets you haven't pushed to the server yet", 22 | "in": "Show incoming changesets that would be pulled in with 'up'", 23 | "rev": "Print the current revision number", 24 | } 25 | 26 | 27 | def parse_action_name(action_name): 28 | """Parse an action name possibly containing arguments. 29 | 30 | Given an `action_name` in the form "name:arg1=val1,arg2=val2" return a 31 | tuple (name, args_dict), where `args_dict` maps from arguments names to 32 | values. 33 | 34 | """ 35 | parts = action_name.split(":") 36 | name = parts[0] 37 | args_str = "" 38 | if len(parts) >= 2: 39 | args_str = parts[1] 40 | 41 | if not args_str: 42 | args_dict = {} 43 | else: 44 | args_list = [a.split("=") for a in args_str.split(",")] 45 | args_dict = dict((a[0], a[1]) for a in args_list) 46 | 47 | return (name, args_dict) 48 | 49 | 50 | def get_action(dirinfo, custom_actions, action_name): 51 | """Return a tuple (action_func, kwargs) or raise RuntimeError if the action is 52 | not found.""" 53 | (action_name, args_dict) = parse_action_name(action_name) 54 | 55 | action_func = getattr(dirinfo, "cmd_" + action_name, None) 56 | if action_func is not None: 57 | return (action_func, args_dict) 58 | 59 | custom_action_func = custom_actions.get(action_name, None) 60 | if custom_action_func is not None: 61 | action_func = partial(custom_action_func, dirinfo) 62 | return (action_func, args_dict) 63 | 64 | raise RuntimeError("Invalid action: " + action_name) 65 | 66 | 67 | def get_custom_actions(): 68 | return dict( 69 | (entrypoint.name, entrypoint.load()) 70 | for entrypoint in pkg_resources.iter_entry_points( 71 | group="checkoutmanager.custom_actions" 72 | ) 73 | ) 74 | 75 | 76 | def execute_action(dirinfo, custom_actions, action): 77 | (action_func, args_dict) = get_action(dirinfo, custom_actions, action) 78 | try: 79 | return action_func(**args_dict) 80 | except utils.CommandError as e: 81 | return e 82 | 83 | 84 | def run_one(action, directory=None, url=None, conf=None, allow_ancestors=True): 85 | custom_actions = get_custom_actions() 86 | if not conf: 87 | conf = config.Config(os.path.expanduser(CONFIGFILE_NAME)) 88 | dir_info = None 89 | if url: 90 | dir_info = conf.directory_from_url(url) 91 | if directory: 92 | if isinstance(directory, DirInfo): 93 | dir_info = directory 94 | else: 95 | dir_info = conf.directory_from_path(directory, allow_ancestors) 96 | if not dir_info: 97 | raise RuntimeError("Could not find the repository for %s!" % (directory or url)) 98 | executor = get_executor(single=True) 99 | executor.execute(execute_action, (dir_info, custom_actions, action)) 100 | executor.wait_for_results() 101 | return executor 102 | 103 | 104 | def run(action, group=None, conf=None, single=False): 105 | custom_actions = get_custom_actions() 106 | if not conf: 107 | conf = config.Config(os.path.expanduser(CONFIGFILE_NAME)) 108 | executor = get_executor(single) 109 | for dirinfo in conf.directories(group=group): 110 | executor.execute(execute_action, (dirinfo, custom_actions, action)) 111 | executor.wait_for_results() 112 | 113 | return executor 114 | 115 | 116 | def main(): 117 | usage = [ 118 | "Usage: %prog action [group]", 119 | " group (optional) is a heading from your config file.", 120 | " action can be " + "/".join(ACTIONS) + ":\n", 121 | ] 122 | # Add automatic action explanations. 123 | usage += [action + "\n " + ACTION_EXPLANATION[action] + "\n" for action in ACTIONS] 124 | usage = "\n".join(usage) 125 | parser = OptionParser(usage=usage) 126 | parser.add_option( 127 | "-v", 128 | "--verbose", 129 | action="store_true", 130 | dest="verbose", 131 | default=False, 132 | help="Show debug output", 133 | ) 134 | parser.add_option( 135 | "-c", 136 | "--configfile", 137 | action="store", 138 | dest="configfile", 139 | default=CONFIGFILE_NAME, 140 | help="Name of config file [%s]" % CONFIGFILE_NAME, 141 | ) 142 | parser.add_option( 143 | "-s", 144 | "--single", 145 | action="store_true", 146 | dest="single", 147 | default=False, 148 | help="Execute actions in a single process", 149 | ) 150 | (options, args) = parser.parse_args() 151 | if options.verbose: 152 | utils.VERBOSE = True 153 | 154 | configfile = os.path.expanduser(options.configfile) 155 | if utils.VERBOSE: 156 | print("Using config file %s" % configfile) 157 | if not os.path.exists(configfile): 158 | print("Config file %s does not exist." % configfile) 159 | sample = pkg_resources.resource_filename("checkoutmanager", "sample.cfg") 160 | shutil.copy(sample, configfile) 161 | print("Copied %s as a sample to %s" % (sample, configfile)) 162 | print("Open it and adjust it to what you need.") 163 | return 164 | 165 | if len(args) < 1: 166 | parser.print_help() 167 | return 168 | action = args[0] 169 | # TODO: check actions 170 | 171 | conf = config.Config(configfile) 172 | 173 | group = None 174 | if len(args) > 1: 175 | group = args[1] 176 | if group not in conf.groupings: 177 | print("Group %s not in %r" % (group, conf.groupings)) 178 | return 179 | 180 | if action == "missing": 181 | print("Looking for items missing in the config file...") 182 | # Special case: report unconfigured items. 183 | conf.report_missing(group=group) 184 | # Also report on not-yet-checked-out items. 185 | print() 186 | print("Looking for not yet checked out items...") 187 | for dirinfo in conf.directories(group=group): 188 | output = dirinfo.cmd_exists(report_only_missing=True) 189 | if output: 190 | print(output) 191 | print("(Run 'checkoutmanager co' if found)") 192 | return 193 | 194 | executor = run(action, group=group, conf=conf, single=options.single) 195 | 196 | if executor.errors: 197 | print() 198 | print("### %s ERRORS OCCURED ###" % len(executor.errors)) 199 | for error in executor.errors: 200 | print() 201 | utils.print_exception(error) 202 | sys.exit(1) 203 | -------------------------------------------------------------------------------- /checkoutmanager/sample.cfg: -------------------------------------------------------------------------------- 1 | # Sample config file. Should be placed as .checkoutmanager.cfg in your home 2 | # directory. 3 | # 4 | # There are different sections per base location and version control 5 | # system. 6 | # 7 | # ``checkoutmanager co`` checks them all out (or clones them). 8 | # ``checkoutmanager up`` updates them all. 9 | # ``checkoutmanager st`` to see if there are uncommitted changes. 10 | # ``checkoutmanager out`` to see if there are unpushed git/hg commits. 11 | 12 | 13 | [git-example] 14 | vcs = git 15 | basedir = ~/example/git 16 | checkouts = 17 | https://github.com/reinout/checkoutmanager 18 | git@github.com:django/django.git 19 | 20 | 21 | [recipes] 22 | # Buildout recipes I work on. 23 | vcs = svn 24 | basedir = ~/example/svn 25 | checkouts = 26 | http://svn.zope.org/repos/main/z3c.recipe.usercrontab/trunk 27 | 28 | 29 | [hg-example] 30 | vcs = hg 31 | basedir = ~/example/utilities 32 | checkouts = 33 | https://bitbucket.org/reinout/eolfixer 34 | https://bitbucket.org/reinout/createcoverage 35 | 36 | 37 | # [dotfolders] 38 | # # Advanced usage! 39 | # # Folders that end up as dotted configfolders in my home dir. 40 | # # Note that there's a directory name behind the repository 41 | # # location, separated by a space. 42 | # vcs = bzr 43 | # basedir = ~ 44 | # checkouts = 45 | # lp:emacsconfig/trunk .emacs.d 46 | # sftp://somewhere/subversion/trunk .subversion 47 | # # By ignoring everything, we do not find missing import files but also 48 | # # don't get warnings for every subdirectory in our home dir 49 | # ignore = 50 | # * 51 | # .* 52 | -------------------------------------------------------------------------------- /checkoutmanager/tests/__init__.py: -------------------------------------------------------------------------------- 1 | # package 2 | -------------------------------------------------------------------------------- /checkoutmanager/tests/config.txt: -------------------------------------------------------------------------------- 1 | .. :doctest: 2 | 3 | The config module reads a config file and massages the data. 4 | 5 | >>> from __future__ import print_function 6 | >>> from __future__ import unicode_literals 7 | >>> from checkoutmanager import config 8 | 9 | Instantiation with a non-exising config file raises and error: 10 | 11 | >>> config.Config('non/existing/configfile') 12 | Traceback (most recent call last): 13 | ... 14 | AssertionError 15 | 16 | Grab the sample config file: 17 | 18 | >>> import pkg_resources 19 | >>> sample_config = pkg_resources.resource_filename( 20 | ... 'checkoutmanager.tests', 'testconfig.cfg') 21 | >>> print(open(sample_config).read()) 22 | # Sample config file. Should be placed as 23 | ... 24 | 25 | Instantiate our config object with the config filename: 26 | 27 | >>> conf = config.Config(sample_config) 28 | 29 | The config file is loaded into a configparser instance: 30 | 31 | >>> conf.parser 32 | <...ConfigParser ...> 33 | 34 | Query for found sections (which we call groupings). Sorted, btw: 35 | 36 | >>> conf.groupings 37 | ['dotfolders', 'recipes'] 38 | 39 | 40 | Determine vcs url and directory name 41 | ------------------------------------ 42 | 43 | There's a helper method for extracting a vcs url and directory name from a 44 | spec. A spec is an url followed by an optional directory name: 45 | 46 | >>> config.extract_spec('aaa bbb') 47 | ('aaa', 'bbb') 48 | >>> config.extract_spec('aaa bbb') 49 | ('aaa', 'bbb') 50 | 51 | More than three parts raises an error: 52 | 53 | >>> config.extract_spec('aaa bbb ccc') 54 | Traceback (most recent call last): 55 | ... 56 | AssertionError: aaa bbb ccc 57 | 58 | If no second part is given, a directory name is extracted from the last path 59 | part of the spec: 60 | 61 | >>> config.extract_spec('aaa') == ('aaa', 'aaa') 62 | True 63 | >>> config.extract_spec('aaa/bbb') == ('aaa/bbb', 'bbb') 64 | True 65 | >>> config.extract_spec('aaa/bbb/') == ('aaa/bbb/', 'bbb') 66 | True 67 | 68 | /trunk is splitted off: 69 | 70 | >>> config.extract_spec('aaa/bbb/trunk') == ('aaa/bbb/trunk', 'bbb') 71 | True 72 | 73 | Unless we specify a directory ourselves: 74 | 75 | >>> config.extract_spec('aaa/bbb/trunk somewhere') == ( 76 | ... 'aaa/bbb/trunk', 'somewhere') 77 | True 78 | 79 | And a branch gets named after the branch: 80 | 81 | >>> config.extract_spec('aaa/bbb/branches/reinout-fix') == ( 82 | ... 'aaa/bbb/branches/reinout-fix', 'bbb-reinout-fix') 83 | True 84 | 85 | Launchpad is also recognized: 86 | 87 | >>> config.extract_spec('lp:myproject') == ('lp:myproject', 'myproject') 88 | True 89 | 90 | Git has some special cases too: 91 | 92 | >>> config.extract_spec('git@github.com:collective/Products.Poi.git') == ( 93 | ... 'git@github.com:collective/Products.Poi.git', 'Products.Poi') 94 | True 95 | >>> config.extract_spec('git@git.example.org:projectname') == ( 96 | ... 'git@git.example.org:projectname', 'projectname') 97 | True 98 | 99 | Preserved tree type checkouts: 100 | 101 | >>> config.extract_spec('svn://some.svn.server/folder1/folder1a/repo1', 102 | ... preserve_tree=["svn://some.svn.server/"]) == ('svn://some.svn.server/folder1/folder1a/repo1', 103 | ... 'folder1/folder1a/repo1') 104 | True 105 | 106 | The default rules like "strip the ``.git`` from the name" still apply to preserved_tree: 107 | 108 | >>> config.extract_spec('git@github.com:reinout/checkoutmanager.git', 109 | ... preserve_tree=["git@github.com"]) == ('git@github.com:reinout/checkoutmanager.git', 110 | ... 'reinout/checkoutmanager') 111 | True 112 | 113 | If not found, preserve_tree doesn't give an error: 114 | 115 | >>> config.extract_spec('git@github.com:reinout/checkoutmanager.git', 116 | ... preserve_tree=["svn://some.svn.server/"]) == ('git@github.com:reinout/checkoutmanager.git', 117 | ... 'checkoutmanager') 118 | True 119 | 120 | 121 | Find directories 122 | ---------------- 123 | 124 | Base purpose: return the wrapped directories: 125 | 126 | >>> for d in conf.directories(): 127 | ... print(d) 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | We can restrict to a certain group: 137 | 138 | >>> for d in conf.directories(group='recipes'): 139 | ... print(d) 140 | 141 | 142 | 143 | 144 | 145 | 146 | We can ignore certain directories and support globbing to do so: 147 | 148 | >>> import os 149 | >>> homedir = getfixture("homedir_in_tmp") 150 | >>> os.mkdir(os.path.join(homedir, 'svn')) 151 | >>> os.mkdir(os.path.join(homedir, 'svn', 'recipes')) 152 | >>> os.mkdir(os.path.join(homedir, 'svn', 'recipes', 'missing')) 153 | >>> os.mkdir(os.path.join(homedir, 'svn', 'recipes', 'ignored_missing')) 154 | >>> conf.report_missing(group='recipes') # doctest: +ELLIPSIS 155 | Unconfigured items in .../svn/recipes [svn]: 156 | .../svn/recipes/missing 157 | 158 | When used as a python module, DirInfo objects can be obtained programmatically: 159 | 160 | >>> conf.directory_from_url('svn://svn/blablabla/trunk') 161 | 162 | >>> conf.directory_from_path('%s/svn/recipes/blablabla' % homedir) 163 | 164 | >>> conf.directory_from_path('%s/svn/recipes/blablabla/somefolder' % homedir) 165 | 166 | -------------------------------------------------------------------------------- /checkoutmanager/tests/conftest.py: -------------------------------------------------------------------------------- 1 | import os 2 | from pathlib import Path 3 | 4 | import pytest 5 | 6 | 7 | @pytest.fixture() 8 | def homedir_in_tmp(tmp_path: Path): 9 | homedir = tmp_path / "homedir" 10 | homedir.mkdir() 11 | orig_home = os.environ["HOME"] 12 | os.environ["HOME"] = str(homedir) 13 | yield homedir 14 | os.environ["HOME"] = orig_home 15 | -------------------------------------------------------------------------------- /checkoutmanager/tests/custom_actions.py: -------------------------------------------------------------------------------- 1 | """Custom actions for testing.""" 2 | 3 | from checkoutmanager import utils 4 | 5 | 6 | def test_action(dirinfo, **kwargs): 7 | print("Test action") 8 | print("dirinfo: %s" % dirinfo) 9 | print( 10 | "arguments: %s" 11 | % ", ".join("%s: %s" % (k, v) for (k, v) in sorted(kwargs.items())) 12 | ) 13 | print(utils.system('echo "echo command executed."')) 14 | -------------------------------------------------------------------------------- /checkoutmanager/tests/custom_actions.txt: -------------------------------------------------------------------------------- 1 | .. :doctest: 2 | 3 | Initial imports: 4 | 5 | >>> from __future__ import print_function 6 | >>> from __future__ import unicode_literals 7 | >>> from checkoutmanager.runner import get_custom_actions, get_action 8 | >>> from checkoutmanager.dirinfo import DirInfo 9 | 10 | In our own ``setup.py``, we have registered an entry point called "test". It 11 | is registerd as a "custom action" entry point. 12 | 13 | Check if abovementioned entry point is recognized by the program runner: 14 | 15 | >>> custom_actions = get_custom_actions() 16 | >>> func = custom_actions['test'] 17 | >>> callable(func) 18 | True 19 | 20 | Check if this action is selected when we pass its name on the command line: 21 | 22 | >>> dirinfo = DirInfo('/some/directory', 'http://some.url') 23 | >>> (action, kwargs) = get_action(dirinfo, custom_actions, 'test') 24 | >>> callable(action) 25 | True 26 | >>> kwargs 27 | {} 28 | 29 | Call the action function: 30 | 31 | >>> action() 32 | Test action 33 | dirinfo: 34 | arguments: 35 | echo command executed. 36 | 37 | Check if arguments are parsed correctly: 38 | 39 | >>> (action, kwargs) = get_action(dirinfo, custom_actions, 'test:arg1=val1,arg2=val2') 40 | >>> callable(action) 41 | True 42 | >>> kwargs['arg1'] == 'val1' 43 | True 44 | >>> kwargs['arg2'] == 'val2' 45 | True 46 | 47 | Call the action function with the arguments: 48 | 49 | >>> action(**kwargs) 50 | Test action 51 | dirinfo: 52 | arguments: arg1: val1, arg2: val2 53 | echo command executed. 54 | 55 | Check if an exception is raised when an invalid action name is given: 56 | 57 | >>> get_action(dirinfo, custom_actions, 'invalid_action_name') 58 | Traceback (most recent call last): 59 | ... 60 | RuntimeError: Invalid action: invalid_action_name 61 | -------------------------------------------------------------------------------- /checkoutmanager/tests/dirinfo.txt: -------------------------------------------------------------------------------- 1 | .. :doctest: 2 | 3 | The dirinfo provides a wrapper for info on one directory: 4 | 5 | >>> from checkoutmanager import dirinfo 6 | 7 | Initialise the wrapper with a directory name: 8 | 9 | >>> info = dirinfo.DirInfo('/non/existing', 'some/url') 10 | 11 | Ask if it exists: 12 | 13 | >>> info.exists 14 | False 15 | 16 | Make an empty directory in the mock homedir and use that: 17 | 18 | >>> homedir = getfixture("homedir_in_tmp") 19 | >>> empty_dir = homedir / "exists" 20 | >>> empty_dir.mkdir() 21 | >>> info = dirinfo.DirInfo(empty_dir, 'some/url') 22 | >>> info.exists 23 | False 24 | 25 | We need one of the ``.git``, ``.hg``-like directories in there: 26 | 27 | >>> dot_dir = empty_dir / ".xxx" 28 | >>> dot_dir.mkdir() 29 | >>> info.exists 30 | True 31 | >>> str(info.directory).endswith("exists") 32 | True 33 | -------------------------------------------------------------------------------- /checkoutmanager/tests/sample.txt: -------------------------------------------------------------------------------- 1 | """ 2 | .. :doctest: 3 | 4 | Sample test: 5 | 6 | >>> 4 + 3 7 | 7 8 | 9 | """ 10 | -------------------------------------------------------------------------------- /checkoutmanager/tests/testconfig.cfg: -------------------------------------------------------------------------------- 1 | # Sample config file. Should be placed as 2 | # .checkoutmanager.cfg in your home directory. 3 | # 4 | # Different sections per base location 5 | # and version control system. Splitting everything all over 6 | # the place in multiple directories is fine. 7 | 8 | [recipes] 9 | # Buildout recipes I work on. 10 | vcs = svn 11 | basedir = ~/svn/recipes 12 | checkouts = 13 | svn://svn/blablabla/trunk 14 | svn://svn/another/trunk differentname 15 | http://host/yetanother/trunk 16 | https://host/yetanother/branches/reinout-fix 17 | https://host/customername/buildout/trunk 18 | ignore = 19 | ignored* 20 | 21 | [dotfolders] 22 | # Advanced usage! 23 | # Folders that end up as dotted configfolders in my home dir. 24 | # Note that there's a directory name behind the repository 25 | # location, separated by a space. 26 | vcs = bzr 27 | basedir = ~ 28 | checkouts = 29 | lp:emacsconfig/trunk .emacs.d 30 | sftp://somwhere/subversion/trunk .subversion 31 | -------------------------------------------------------------------------------- /checkoutmanager/tests/utils.txt: -------------------------------------------------------------------------------- 1 | .. :doctest: 2 | 3 | Initial imports: 4 | 5 | >>> from __future__ import print_function 6 | >>> from __future__ import unicode_literals 7 | >>> from checkoutmanager import utils 8 | 9 | Very basic testing of utils.py: basically just a command line runner: 10 | 11 | >>> output = utils.system('echo "hello"') 12 | >>> 'hello' in str(output) 13 | True 14 | 15 | >>> try: 16 | ... utils.system('non_existing_command') 17 | ... print("Exception not raised") 18 | ... except utils.CommandError: 19 | ... print("Exception properly raised") 20 | Exception properly raised 21 | 22 | >>> try: 23 | ... utils.system('non_existing_command') 24 | ... except utils.CommandError as e: 25 | ... print(e.format_msg()) 26 | Something went wrong when executing: 27 | non_existing_command 28 | while in directory: 29 | /...checkoutmanager 30 | Returncode: 31 | 127 32 | Output: 33 | ... not found 34 | 35 | CommandError needs to be pickleable: 36 | 37 | >>> import pickle 38 | 39 | >>> e1 = utils.CommandError(1, "cmd", "out") 40 | >>> e2 = pickle.loads(pickle.dumps(e1)) 41 | >>> for attr in ("returncode", "command", "output", "working_dir"): 42 | ... print(getattr(e1, attr) == getattr(e2, attr)) 43 | True 44 | True 45 | True 46 | True 47 | 48 | More verbose output: 49 | 50 | >>> utils.VERBOSE = True 51 | >>> output = utils.system('echo "hello"') 52 | [...] echo "hello" 53 | >>> 'hello' in str(output) 54 | True 55 | 56 | Teardown: 57 | 58 | >>> utils.VERBOSE = False 59 | -------------------------------------------------------------------------------- /checkoutmanager/utils.py: -------------------------------------------------------------------------------- 1 | import os 2 | import subprocess 3 | import sys 4 | import traceback 5 | from functools import wraps 6 | from io import StringIO 7 | 8 | # For zc.buildout's system() method: 9 | MUST_CLOSE_FDS = not sys.platform.startswith("win") 10 | # When you set '-v', this constant is changed. A bit hacky. 11 | VERBOSE = False 12 | 13 | 14 | class CommandError(Exception): 15 | def __init__(self, returncode=0, command="", output=""): 16 | self.returncode = returncode 17 | self.command = command 18 | self.output = output 19 | self.working_dir = os.getcwd() 20 | 21 | def format_msg(self): 22 | lines = [] 23 | lines.append("Something went wrong when executing:") 24 | lines.append(" %s" % self.command) 25 | lines.append("while in directory:") 26 | lines.append(" %s" % self.working_dir) 27 | lines.append("Returncode:") 28 | lines.append(" %s" % self.returncode) 29 | lines.append("Output:") 30 | lines.append(self.output) 31 | return "\n".join(lines) 32 | 33 | def print_msg(self): 34 | print(self.format_msg()) 35 | 36 | 37 | def system(command, input=None): 38 | """commands.getoutput() replacement that also works on windows 39 | 40 | Code copied from zc.buildout. 41 | 42 | """ 43 | if VERBOSE: 44 | print("[%s] %s" % (os.getcwd(), command)) 45 | p = subprocess.Popen( 46 | command, 47 | shell=True, 48 | stdin=subprocess.PIPE, 49 | stdout=subprocess.PIPE, 50 | stderr=subprocess.PIPE, 51 | close_fds=MUST_CLOSE_FDS, 52 | ) 53 | if input: 54 | input = input.encode() 55 | stdoutdata, stderrdata = p.communicate(input=input) 56 | result = stdoutdata + stderrdata 57 | result = result.decode() 58 | if p.returncode: 59 | raise CommandError(p.returncode, command, result) 60 | 61 | return result 62 | 63 | 64 | def capture_stdout(func): 65 | """Decorator to capture stdout and return it as a string. 66 | 67 | NOTE: The return value of the wrapped function is discarded. 68 | """ 69 | import sys 70 | 71 | @wraps(func) 72 | def newfunc(*args, **kwargs): 73 | sys.stdout = StringIO() 74 | try: 75 | func(*args, **kwargs) 76 | return sys.stdout.getvalue() 77 | finally: 78 | sys.stdout = sys.__stdout__ 79 | 80 | return newfunc 81 | 82 | 83 | def print_exception(exception): 84 | if isinstance(exception, CommandError): 85 | result = exception.format_msg() 86 | else: 87 | result = "".join(traceback.format_exception_only(type(exception), exception)) 88 | # Don't print empty lines 89 | if result: 90 | print(result) 91 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.ruff] 2 | # See https://docs.astral.sh/ruff/configuration/ for defaults. 3 | target-version = "py38" 4 | 5 | [tool.ruff.lint] 6 | # Default select: ["E4", "E7", "E9", "F"] 7 | select = ["E4", "E7", "E9", "F", "I", "UP"] 8 | ignore = ["UP031"] 9 | 10 | [tool.ruff.lint.isort] 11 | known-first-party = ["checkoutmanager"] 12 | 13 | [tool.pytest.ini_options] 14 | doctest_optionflags = "NORMALIZE_WHITESPACE ELLIPSIS" 15 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | -e .[test] 2 | ruff 3 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from setuptools import setup 4 | 5 | version = "3.2.dev0" 6 | 7 | sample_config = open(os.path.join("checkoutmanager", "sample.cfg")).readlines() 8 | 9 | long_description = "\n\n".join( 10 | [ 11 | open("README.rst").read(), 12 | "\n".join([" " + line.rstrip() for line in sample_config]), 13 | open("TODO.rst").read(), 14 | open("CREDITS.rst").read(), 15 | open("CHANGES.rst").read(), 16 | ] 17 | ) 18 | 19 | setup( 20 | name="checkoutmanager", 21 | version=version, 22 | description=( 23 | "Gives you overview and control over your " + "git/hg/bzr/svn checkouts/clones." 24 | ), 25 | long_description=long_description, 26 | # Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers 27 | classifiers=[ 28 | "Development Status :: 6 - Mature", 29 | "Environment :: Console", 30 | "License :: OSI Approved :: GNU General Public License (GPL)", 31 | "Operating System :: MacOS :: MacOS X", 32 | "Operating System :: Microsoft :: Windows", 33 | "Operating System :: POSIX", 34 | "Programming Language :: Python :: 3", 35 | ], 36 | keywords=[], 37 | author="Reinout van Rees", 38 | author_email="reinout@vanrees.org", 39 | url="http://reinout.vanrees.org", 40 | license="GPL", 41 | packages=["checkoutmanager"], 42 | include_package_data=True, 43 | zip_safe=False, 44 | install_requires=[ 45 | "setuptools", 46 | ], 47 | extras_require={ 48 | "test": [ 49 | "pytest", 50 | ], 51 | }, 52 | entry_points={ 53 | "console_scripts": [ 54 | "checkoutmanager = checkoutmanager.runner:main", 55 | ], 56 | "checkoutmanager.custom_actions": [ 57 | "test = checkoutmanager.tests.custom_actions:test_action", 58 | ], 59 | }, 60 | ) 61 | --------------------------------------------------------------------------------