mercurial/posix.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Mon, 02 Nov 2020 11:03:56 +0100
changeset 45907 06b64fabf91c
parent 45718 87c35b5a14eb
child 45957 89a2afe31e82
permissions -rw-r--r--
copies: cache the ancestor checking call when tracing copy A good share of the time spent in this function is spent doing ancestors checking. To avoid spending time in duplicated call, we cache the result of calls. In the slower case, this provide a quite significant performance boost. Below are the result for a set of selected pairs (many of them pathological): (And further down is another table that summarize the current state of filelog based vs changeset base copy tracing) The benchmark have been configured to be killed after 6 minutes of runtime, which mean that any detect slower than 2 minutes will be marked as "killed". This drop some useful information about how much slower these case are… but also prevent 99% of the benchmark time to be spent on case that can be labelled "very slow" anyway. Repo Case Source-Rev Dest-Rev Old-Time New-Time Difference Factor ------------------------------------------------------------------------------------------------------------------------------------ mercurial x_revs_x_added_0_copies ad6b123de1c7 39cfcef4f463 : 0.000044 s, 0.000044 s, +0.000000 s, × 1.0000 mercurial x_revs_x_added_x_copies 2b1c78674230 0c1d10351869 : 0.000138 s, 0.000138 s, +0.000000 s, × 1.0000 mercurial x000_revs_x000_added_x_copies 81f8ff2a9bf2 dd3267698d84 : 0.005067 s, 0.005052 s, -0.000015 s, × 0.9970 pypy x_revs_x_added_0_copies aed021ee8ae8 099ed31b181b : 0.000218 s, 0.000219 s, +0.000001 s, × 1.0046 pypy x_revs_x000_added_0_copies 4aa4e1f8e19a 359343b9ac0e : 0.000053 s, 0.000055 s, +0.000002 s, × 1.0377 pypy x_revs_x_added_x_copies ac52eb7bbbb0 72e022663155 : 0.000125 s, 0.000128 s, +0.000003 s, × 1.0240 pypy x_revs_x00_added_x_copies c3b14617fbd7 ace7255d9a26 : 0.001098 s, 0.001089 s, -0.000009 s, × 0.9918 pypy x_revs_x000_added_x000_copies df6f7a526b60 a83dc6a2d56f : 0.017546 s, 0.017407 s, -0.000139 s, × 0.9921 pypy x000_revs_xx00_added_0_copies 89a76aede314 2f22446ff07e : 0.096723 s, 0.094175 s, -0.002548 s, × 0.9737 pypy x000_revs_x000_added_x_copies 8a3b5bfd266e 2c68e87c3efe : 0.271796 s, 0.238009 s, -0.033787 s, × 0.8757 pypy x000_revs_x000_added_x000_copies 89a76aede314 7b3dda341c84 : 0.128602 s, 0.125876 s, -0.002726 s, × 0.9788 pypy x0000_revs_x_added_0_copies d1defd0dc478 c9cb1334cc78 : 7.086742 s, 3.581556 s, -3.505186 s, × 0.5054 pypy x0000_revs_xx000_added_0_copies bf2c629d0071 4ffed77c095c : 0.016634 s, 0.016721 s, +0.000087 s, × 1.0052 pypy x0000_revs_xx000_added_x000_copies 08ea3258278e d9fa043f30c0 : 0.254225 s, 0.242367 s, -0.011858 s, × 0.9534 netbeans x_revs_x_added_0_copies fb0955ffcbcd a01e9239f9e7 : 0.000166 s, 0.000165 s, -0.000001 s, × 0.9940 netbeans x_revs_x000_added_0_copies 6f360122949f 20eb231cc7d0 : 0.000118 s, 0.000114 s, -0.000004 s, × 0.9661 netbeans x_revs_x_added_x_copies 1ada3faf6fb6 5a39d12eecf4 : 0.000296 s, 0.000296 s, +0.000000 s, × 1.0000 netbeans x_revs_x00_added_x_copies 35be93ba1e2c 9eec5e90c05f : 0.001137 s, 0.001124 s, -0.000013 s, × 0.9886 netbeans x000_revs_xx00_added_0_copies eac3045b4fdd 51d4ae7f1290 : 0.014133 s, 0.013060 s, -0.001073 s, × 0.9241 netbeans x000_revs_x000_added_x_copies e2063d266acd 6081d72689dc : 0.016988 s, 0.017112 s, +0.000124 s, × 1.0073 netbeans x000_revs_x000_added_x000_copies ff453e9fee32 411350406ec2 : 0.676361 s, 0.660350 s, -0.016011 s, × 0.9763 netbeans x0000_revs_xx000_added_x000_copies 588c2d1ced70 1aad62e59ddd : 12.515149 s, 10.032499 s, -2.482650 s, × 0.8016 mozilla-central x_revs_x_added_0_copies 3697f962bb7b 7015fcdd43a2 : 0.000186 s, 0.000189 s, +0.000003 s, × 1.0161 mozilla-central x_revs_x000_added_0_copies dd390860c6c9 40d0c5bed75d : 0.000459 s, 0.000462 s, +0.000003 s, × 1.0065 mozilla-central x_revs_x_added_x_copies 8d198483ae3b 14207ffc2b2f : 0.000273 s, 0.000270 s, -0.000003 s, × 0.9890 mozilla-central x_revs_x00_added_x_copies 98cbc58cc6bc 446a150332c3 : 0.001503 s, 0.001474 s, -0.000029 s, × 0.9807 mozilla-central x_revs_x000_added_x000_copies 3c684b4b8f68 0a5e72d1b479 : 0.004862 s, 0.004806 s, -0.000056 s, × 0.9885 mozilla-central x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 0.088291 s, 0.085150 s, -0.003141 s, × 0.9644 mozilla-central x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.007113 s, 0.007064 s, -0.000049 s, × 0.9931 mozilla-central x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.004687 s, 0.004741 s, +0.000054 s, × 1.0115 mozilla-central x000_revs_x000_added_x000_copies 7c97034feb78 4407bd0c6330 : 0.198710 s, 0.190133 s, -0.008577 s, × 0.9568 mozilla-central x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 0.036068 s, 0.035651 s, -0.000417 s, × 0.9884 mozilla-central x0000_revs_xx000_added_x000_copies f78c615a656c 96a38b690156 : 0.465362 s, 0.440694 s, -0.024668 s, × 0.9470 mozilla-central x00000_revs_x0000_added_x0000_copies 6832ae71433c 4c222a1d9a00 : 24.519684 s, 18.454163 s, -6.065521 s, × 0.7526 mozilla-central x00000_revs_x00000_added_x000_copies 76caed42cf7c 1daa622bbe42 : 42.711897 s, 31.562719 s, -11.149178 s, × 0.7390 mozilla-try x_revs_x_added_0_copies aaf6dde0deb8 9790f499805a : 0.001201 s, 0.001189 s, -0.000012 s, × 0.9900 mozilla-try x_revs_x000_added_0_copies d8d0222927b4 5bb8ce8c7450 : 0.001216 s, 0.001204 s, -0.000012 s, × 0.9901 mozilla-try x_revs_x_added_x_copies 092fcca11bdb 936255a0384a : 0.000595 s, 0.000586 s, -0.000009 s, × 0.9849 mozilla-try x_revs_x00_added_x_copies b53d2fadbdb5 017afae788ec : 0.001856 s, 0.001845 s, -0.000011 s, × 0.9941 mozilla-try x_revs_x000_added_x000_copies 20408ad61ce5 6f0ee96e21ad : 0.064936 s, 0.063822 s, -0.001114 s, × 0.9828 mozilla-try x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 0.090601 s, 0.088038 s, -0.002563 s, × 0.9717 mozilla-try x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.007510 s, 0.007389 s, -0.000121 s, × 0.9839 mozilla-try x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.004911 s, 0.004868 s, -0.000043 s, × 0.9912 mozilla-try x000_revs_x000_added_x000_copies 1346fd0130e4 4c65cbdabc1f : 0.233231 s, 0.222450 s, -0.010781 s, × 0.9538 mozilla-try x0000_revs_x_added_0_copies 63519bfd42ee a36a2a865d92 : 0.419989 s, 0.370675 s, -0.049314 s, × 0.8826 mozilla-try x0000_revs_x_added_x_copies 9fe69ff0762d bcabf2a78927 : 0.401521 s, 0.358020 s, -0.043501 s, × 0.8917 mozilla-try x0000_revs_xx000_added_x_copies 156f6e2674f2 4d0f2c178e66 : 0.179555 s, 0.145235 s, -0.034320 s, × 0.8089 mozilla-try x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 0.038004 s, 0.037606 s, -0.000398 s, × 0.9895 mozilla-try x0000_revs_xx000_added_x000_copies 89294cd501d9 7ccb2fc7ccb5 : 52.838482 s, 7.382439 s, -45.456043 s, × 0.1397 mozilla-try x0000_revs_x0000_added_x0000_copies e928c65095ed e951f4ad123a : 8.705874 s, 7.273506 s, -1.432368 s, × 0.8355 mozilla-try x00000_revs_x00000_added_0_copies dc8a3ca7010e d16fde900c9c : 1.126708 s, 1.074593 s, -0.052115 s, × 0.9537 mozilla-try x00000_revs_x0000_added_x0000_copies 8d3fafa80d4b eb884023b810 : 83.854020 s, 27.746195 s, -56.107825 s, × 0.3309 Below is a table comparing the runtime of the current "filelog centric" algorithm, with the "changeset centric" one, we just modified. The changeset centric algorithm is a significant win in many scenario, but they are still various cases where it is quite slower. When many revision has to be considered the cost of retrieving the copy information, creating new dictionaries, merging dictionaries and checking if revision are ancestors of each other can slow things down. The rest of this series, will introduce a rust version of the copy tracing code to deal with most of theses issues. Repo Case Source-Rev Dest-Rev filelog sidedata Difference Factor --------------------------------------------------------------------------------------------------------------------------------------- mercurial x_revs_x_added_0_copies ad6b123de1c7 39cfcef4f463 : 0.000914 s, 0.000044 s, - 0.000870 s, × 0.048140 mercurial x_revs_x_added_x_copies 2b1c78674230 0c1d10351869 : 0.001812 s, 0.000138 s, - 0.001674 s, × 0.076159 mercurial x000_revs_x000_added_x_copies 81f8ff2a9bf2 dd3267698d84 : 0.017954 s, 0.005052 s, - 0.012902 s, × 0.281386 pypy x_revs_x_added_0_copies aed021ee8ae8 099ed31b181b : 0.001509 s, 0.000219 s, - 0.001290 s, × 0.145129 pypy x_revs_x000_added_0_copies 4aa4e1f8e19a 359343b9ac0e : 0.206881 s, 0.000055 s, - 0.206826 s, × 0.000266 pypy x_revs_x_added_x_copies ac52eb7bbbb0 72e022663155 : 0.016951 s, 0.000128 s, - 0.016823 s, × 0.007551 pypy x_revs_x00_added_x_copies c3b14617fbd7 ace7255d9a26 : 0.019096 s, 0.001089 s, - 0.018007 s, × 0.057028 pypy x_revs_x000_added_x000_copies df6f7a526b60 a83dc6a2d56f : 0.762506 s, 0.017407 s, - 0.745099 s, × 0.022829 pypy x000_revs_xx00_added_0_copies 89a76aede314 2f22446ff07e : 1.179211 s, 0.094175 s, - 1.085036 s, × 0.079863 pypy x000_revs_x000_added_x_copies 8a3b5bfd266e 2c68e87c3efe : 1.249058 s, 0.238009 s, - 1.011049 s, × 0.190551 pypy x000_revs_x000_added_x000_copies 89a76aede314 7b3dda341c84 : 1.614107 s, 0.125876 s, - 1.488231 s, × 0.077985 pypy x0000_revs_x_added_0_copies d1defd0dc478 c9cb1334cc78 : 0.001064 s, 3.581556 s, + 3.580492 s, × 3366.124060 pypy x0000_revs_xx000_added_0_copies bf2c629d0071 4ffed77c095c : 1.061275 s, 0.016721 s, - 1.044554 s, × 0.015756 pypy x0000_revs_xx000_added_x000_copies 08ea3258278e d9fa043f30c0 : 1.341119 s, 0.242367 s, - 1.098752 s, × 0.180720 netbeans x_revs_x_added_0_copies fb0955ffcbcd a01e9239f9e7 : 0.027803 s, 0.000165 s, - 0.027638 s, × 0.005935 netbeans x_revs_x000_added_0_copies 6f360122949f 20eb231cc7d0 : 0.130014 s, 0.000114 s, - 0.129900 s, × 0.000877 netbeans x_revs_x_added_x_copies 1ada3faf6fb6 5a39d12eecf4 : 0.024990 s, 0.000296 s, - 0.024694 s, × 0.011845 netbeans x_revs_x00_added_x_copies 35be93ba1e2c 9eec5e90c05f : 0.052201 s, 0.001124 s, - 0.051077 s, × 0.021532 netbeans x000_revs_xx00_added_0_copies eac3045b4fdd 51d4ae7f1290 : 0.037642 s, 0.013060 s, - 0.024582 s, × 0.346953 netbeans x000_revs_x000_added_x_copies e2063d266acd 6081d72689dc : 0.197086 s, 0.017112 s, - 0.179974 s, × 0.086825 netbeans x000_revs_x000_added_x000_copies ff453e9fee32 411350406ec2 : 0.935148 s, 0.660350 s, - 0.274798 s, × 0.706145 netbeans x0000_revs_xx000_added_x000_copies 588c2d1ced70 1aad62e59ddd : 3.920674 s, 10.032499 s, + 6.111825 s, × 2.558871 mozilla-central x_revs_x_added_0_copies 3697f962bb7b 7015fcdd43a2 : 0.024232 s, 0.000189 s, - 0.024043 s, × 0.007800 mozilla-central x_revs_x000_added_0_copies dd390860c6c9 40d0c5bed75d : 0.141483 s, 0.000462 s, - 0.141021 s, × 0.003265 mozilla-central x_revs_x_added_x_copies 8d198483ae3b 14207ffc2b2f : 0.025775 s, 0.000270 s, - 0.025505 s, × 0.010475 mozilla-central x_revs_x00_added_x_copies 98cbc58cc6bc 446a150332c3 : 0.084922 s, 0.001474 s, - 0.083448 s, × 0.017357 mozilla-central x_revs_x000_added_x000_copies 3c684b4b8f68 0a5e72d1b479 : 0.194784 s, 0.004806 s, - 0.189978 s, × 0.024673 mozilla-central x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 2.161103 s, 0.085150 s, - 2.075953 s, × 0.039401 mozilla-central x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.089347 s, 0.007064 s, - 0.082283 s, × 0.079063 mozilla-central x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.732171 s, 0.004741 s, - 0.727430 s, × 0.006475 mozilla-central x000_revs_x000_added_x000_copies 7c97034feb78 4407bd0c6330 : 1.157287 s, 0.190133 s, - 0.967154 s, × 0.164292 mozilla-central x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 6.726568 s, 0.035651 s, - 6.690917 s, × 0.005300 mozilla-central x0000_revs_xx000_added_x000_copies f78c615a656c 96a38b690156 : 3.266229 s, 0.440694 s, - 2.825535 s, × 0.134924 mozilla-central x00000_revs_x0000_added_x0000_copies 6832ae71433c 4c222a1d9a00 : 15.860534 s, 18.454163 s, + 2.593629 s, × 1.163527 mozilla-central x00000_revs_x00000_added_x000_copies 76caed42cf7c 1daa622bbe42 : 20.450475 s, 31.562719 s, +11.112244 s, × 1.543373 mozilla-try x_revs_x_added_0_copies aaf6dde0deb8 9790f499805a : 0.080442 s, 0.001189 s, - 0.079253 s, × 0.014781 mozilla-try x_revs_x000_added_0_copies d8d0222927b4 5bb8ce8c7450 : 0.497672 s, 0.001204 s, - 0.496468 s, × 0.002419 mozilla-try x_revs_x_added_x_copies 092fcca11bdb 936255a0384a : 0.021183 s, 0.000586 s, - 0.020597 s, × 0.027664 mozilla-try x_revs_x00_added_x_copies b53d2fadbdb5 017afae788ec : 0.230991 s, 0.001845 s, - 0.229146 s, × 0.007987 mozilla-try x_revs_x000_added_x000_copies 20408ad61ce5 6f0ee96e21ad : 1.118461 s, 0.063822 s, - 1.054639 s, × 0.057062 mozilla-try x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 2.206083 s, 0.088038 s, - 2.118045 s, × 0.039907 mozilla-try x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.089404 s, 0.007389 s, - 0.082015 s, × 0.082647 mozilla-try x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.733043 s, 0.004868 s, - 0.728175 s, × 0.006641 mozilla-try x000_revs_x000_added_x000_copies 1346fd0130e4 4c65cbdabc1f : 1.163367 s, 0.222450 s, - 0.940917 s, × 0.191212 mozilla-try x0000_revs_x_added_0_copies 63519bfd42ee a36a2a865d92 : 0.085456 s, 0.370675 s, + 0.285219 s, × 4.337612 mozilla-try x0000_revs_x_added_x_copies 9fe69ff0762d bcabf2a78927 : 0.083601 s, 0.358020 s, + 0.274419 s, × 4.282485 mozilla-try x0000_revs_xx000_added_x_copies 156f6e2674f2 4d0f2c178e66 : 7.366614 s, 0.145235 s, - 7.221379 s, × 0.019715 mozilla-try x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 6.664464 s, 0.037606 s, - 6.626858 s, × 0.005643 mozilla-try x0000_revs_xx000_added_x000_copies 89294cd501d9 7ccb2fc7ccb5 : 7.467836 s, 7.382439 s, - 0.085397 s, × 0.988565 mozilla-try x0000_revs_x0000_added_x0000_copies e928c65095ed e951f4ad123a : 9.801294 s, 7.273506 s, - 2.527788 s, × 0.742097 mozilla-try x00000_revs_x_added_0_copies 6a320851d377 1ebb79acd503 : 0.091886 s, killed mozilla-try x00000_revs_x00000_added_0_copies dc8a3ca7010e d16fde900c9c : 26.491140 s, 1.074593 s, -25.416547 s, × 0.040564 mozilla-try x00000_revs_x_added_x_copies 5173c4b6f97c 95d83ee7242d : 0.092863 s, killed mozilla-try x00000_revs_x000_added_x_copies 9126823d0e9c ca82787bb23c : 0.226823 s, killed mozilla-try x00000_revs_x0000_added_x0000_copies 8d3fafa80d4b eb884023b810 : 18.914630 s, 27.746195 s, + 8.831565 s, × 1.466917 mozilla-try x00000_revs_x00000_added_x0000_copies 1b661134e2ca 1ae03d022d6d : 21.198903 s, killed mozilla-try x00000_revs_x00000_added_x000_copies 9b2a99adc05e 8e29777b48e6 : 24.952268 s, killed Differential Revision: https://phab.mercurial-scm.org/D9296
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8226
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
     1
# posix.py - Posix utility function implementations for Mercurial
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
     2
#
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
     3
#  Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
     4
#
8b2cd04a6e97 put license and copyright info into comment blocks
Martin Geisler <mg@lazybytes.net>
parents: 8225
diff changeset
     5
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10218
diff changeset
     6
# GNU General Public License version 2 or any later version.
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     7
25967
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
     8
from __future__ import absolute_import
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
     9
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    10
import errno
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    11
import fcntl
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    12
import getpass
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    13
import grp
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    14
import os
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    15
import pwd
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    16
import re
25420
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
    17
import select
25967
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    18
import stat
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    19
import sys
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    20
import tempfile
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    21
import unicodedata
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    22
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    23
from .i18n import _
43089
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43085
diff changeset
    24
from .pycompat import (
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43085
diff changeset
    25
    getattr,
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43085
diff changeset
    26
    open,
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43085
diff changeset
    27
)
25967
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    28
from . import (
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    29
    encoding,
33649
739cc0f9cbb4 ssh: ban any username@host or host that starts with - (SEC)
Augie Fackler <augie@google.com>
parents: 32741
diff changeset
    30
    error,
35513
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
    31
    policy,
30617
d623cc6b3742 py3: replace os.pathsep with pycompat.ospathsep
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30561
diff changeset
    32
    pycompat,
25967
224a33452ed4 posix: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25660
diff changeset
    33
)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    34
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43525
diff changeset
    35
osutil = policy.importmod('osutil')
35513
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
    36
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    37
normpath = os.path.normpath
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    38
samestat = os.path.samestat
27236
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    39
try:
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    40
    oslink = os.link
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    41
except AttributeError:
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    42
    # Some platforms build Python without os.link on systems that are
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    43
    # vaguely unix-like but don't have hardlink support. For those
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    44
    # poor souls, just say we tried and that it failed so we fall back
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    45
    # to copies.
b0d90fef16b6 posix: work around "posix" systems without os.link available (issue4974)
Augie Fackler <augie@google.com>
parents: 26901
diff changeset
    46
    def oslink(src, dst):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
    47
        raise OSError(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    48
            errno.EINVAL, b'hardlinks not supported: %s to %s' % (src, dst)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
    49
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
    50
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
    51
39909
5fe0b880200e py3: convert os.readlink() path to native strings on Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 38264
diff changeset
    52
readlink = os.readlink
13280
6052bbc7aabd reintroduces util.unlink, for POSIX and Windows.
Adrian Buehlmann <adrian@cadifra.com>
parents: 13007
diff changeset
    53
unlink = os.unlink
9549
8b8920209317 util: move rename into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 9517
diff changeset
    54
rename = os.rename
24692
144883a8d0d4 util: add removedirs as platform depending function
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24596
diff changeset
    55
removedirs = os.removedirs
8614
573734e7e6d0 cmdutils: Take over glob expansion duties from util
Matt Mackall <mpm@selenic.com>
parents: 8312
diff changeset
    56
expandglobs = False
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    57
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    58
umask = os.umask(0)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    59
os.umask(umask)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    60
42189
97ada9b8d51b posix: always seek to EOF when opening a file in append mode
Augie Fackler <augie@google.com>
parents: 41832
diff changeset
    61
if not pycompat.ispy3:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
    62
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43525
diff changeset
    63
    def posixfile(name, mode='r', buffering=-1):
42189
97ada9b8d51b posix: always seek to EOF when opening a file in append mode
Augie Fackler <augie@google.com>
parents: 41832
diff changeset
    64
        fp = open(name, mode=mode, buffering=buffering)
97ada9b8d51b posix: always seek to EOF when opening a file in append mode
Augie Fackler <augie@google.com>
parents: 41832
diff changeset
    65
        # The position when opening in append mode is implementation defined, so
97ada9b8d51b posix: always seek to EOF when opening a file in append mode
Augie Fackler <augie@google.com>
parents: 41832
diff changeset
    66
        # make it consistent by always seeking to the end.
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43525
diff changeset
    67
        if 'a' in mode:
42189
97ada9b8d51b posix: always seek to EOF when opening a file in append mode
Augie Fackler <augie@google.com>
parents: 41832
diff changeset
    68
            fp.seek(0, os.SEEK_END)
97ada9b8d51b posix: always seek to EOF when opening a file in append mode
Augie Fackler <augie@google.com>
parents: 41832
diff changeset
    69
        return fp
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
    70
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
    71
42189
97ada9b8d51b posix: always seek to EOF when opening a file in append mode
Augie Fackler <augie@google.com>
parents: 41832
diff changeset
    72
else:
97ada9b8d51b posix: always seek to EOF when opening a file in append mode
Augie Fackler <augie@google.com>
parents: 41832
diff changeset
    73
    # The underlying file object seeks as required in Python 3:
97ada9b8d51b posix: always seek to EOF when opening a file in append mode
Augie Fackler <augie@google.com>
parents: 41832
diff changeset
    74
    # https://github.com/python/cpython/blob/v3.7.3/Modules/_io/fileio.c#L474
97ada9b8d51b posix: always seek to EOF when opening a file in append mode
Augie Fackler <augie@google.com>
parents: 41832
diff changeset
    75
    posixfile = open
97ada9b8d51b posix: always seek to EOF when opening a file in append mode
Augie Fackler <augie@google.com>
parents: 41832
diff changeset
    76
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
    77
17560
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
    78
def split(p):
18288
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    79
    '''Same as posixpath.split, but faster
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    80
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    81
    >>> import posixpath
34146
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33657
diff changeset
    82
    >>> for f in [b'/absolute/path/to/file',
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33657
diff changeset
    83
    ...           b'relative/path/to/file',
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33657
diff changeset
    84
    ...           b'file_alone',
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33657
diff changeset
    85
    ...           b'path/to/directory/',
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33657
diff changeset
    86
    ...           b'/multiple/path//separators',
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33657
diff changeset
    87
    ...           b'/file_at_root',
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33657
diff changeset
    88
    ...           b'///multiple_leading_separators_at_root',
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33657
diff changeset
    89
    ...           b'']:
18288
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    90
    ...     assert split(f) == posixpath.split(f), f
0d5a22f73a1f posix: fix split() for the case where the path is at the root of the filesystem
Remy Blank <remy.blank@pobox.com>
parents: 18143
diff changeset
    91
    '''
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    92
    ht = p.rsplit(b'/', 1)
17560
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
    93
    if len(ht) == 1:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    94
        return b'', p
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    95
    nh = ht[0].rstrip(b'/')
17560
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
    96
    if nh:
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
    97
        return nh, ht[1]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    98
    return ht[0] + b'/', ht[1]
17560
9ee25d7b1aed util: implement a faster os.path.split for posix systems
Bryan O'Sullivan <bryano@fb.com>
parents: 17391
diff changeset
    99
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   100
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   101
def openhardlinks():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   102
    '''return true if it is safe to hold open file handles to hardlinks'''
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   103
    return True
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   104
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   105
13375
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13280
diff changeset
   106
def nlinks(name):
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13280
diff changeset
   107
    '''return number of hardlinks for the given file'''
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13280
diff changeset
   108
    return os.lstat(name).st_nlink
f1fa8f481c7c port win32.py to using the Python ctypes library
Adrian Buehlmann <adrian@cadifra.com>
parents: 13280
diff changeset
   109
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   110
14231
8abe4db2d162 rename util.parse_patch_output to parsepatchoutput
Adrian Buehlmann <adrian@cadifra.com>
parents: 14165
diff changeset
   111
def parsepatchoutput(output_line):
8761
0289f384e1e5 Generally replace "file name" with "filename" in help and comments.
timeless <timeless@gmail.com>
parents: 8657
diff changeset
   112
    """parses the output produced by patch and returns the filename"""
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   113
    pf = output_line[14:]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   114
    if pycompat.sysplatform == b'OpenVMS':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   115
        if pf[0] == b'`':
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   116
            pf = pf[1:-1]  # Remove the quotes
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   117
    else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   118
        if pf.startswith(b"'") and pf.endswith(b"'") and b" " in pf:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   119
            pf = pf[1:-1]  # Remove the quotes
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   120
    return pf
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   121
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   122
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   123
def sshargs(sshcmd, host, user, port):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   124
    '''Build argument list for ssh'''
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   125
    args = user and (b"%s@%s" % (user, host)) or host
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   126
    if b'-' in args[:1]:
33649
739cc0f9cbb4 ssh: ban any username@host or host that starts with - (SEC)
Augie Fackler <augie@google.com>
parents: 32741
diff changeset
   127
        raise error.Abort(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   128
            _(b'illegal ssh hostname or username starting with -: %s') % args
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   129
        )
33657
8cb9e921ef8c ssh: quote parameters using shellquote (SEC)
Jun Wu <quark@fb.com>
parents: 33649
diff changeset
   130
    args = shellquote(args)
8cb9e921ef8c ssh: quote parameters using shellquote (SEC)
Jun Wu <quark@fb.com>
parents: 33649
diff changeset
   131
    if port:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   132
        args = b'-p %s %s' % (shellquote(port), args)
33657
8cb9e921ef8c ssh: quote parameters using shellquote (SEC)
Jun Wu <quark@fb.com>
parents: 33649
diff changeset
   133
    return args
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   134
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   135
14273
38af0f514134 rename util.is_exec to isexec
Adrian Buehlmann <adrian@cadifra.com>
parents: 14272
diff changeset
   136
def isexec(f):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   137
    """check whether a file is executable"""
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   138
    return os.lstat(f).st_mode & 0o100 != 0
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   139
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   140
14232
df2399663392 rename util.set_flags to setflags
Adrian Buehlmann <adrian@cadifra.com>
parents: 14231
diff changeset
   141
def setflags(f, l, x):
32741
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32432
diff changeset
   142
    st = os.lstat(f)
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32432
diff changeset
   143
    s = st.st_mode
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   144
    if l:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   145
        if not stat.S_ISLNK(s):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   146
            # switch file to link
45717
755214a84b9d posix: use context managers in a couple of places
Matt Harbison <matt_harbison@yahoo.com>
parents: 44926
diff changeset
   147
            with open(f, b'rb') as fp:
755214a84b9d posix: use context managers in a couple of places
Matt Harbison <matt_harbison@yahoo.com>
parents: 44926
diff changeset
   148
                data = fp.read()
31543
c6cbe5720353 posix: use local reference to unlink
Ryan McElroy <rmcelroy@fb.com>
parents: 31513
diff changeset
   149
            unlink(f)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   150
            try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   151
                os.symlink(data, f)
14004
97ed99d1f419 eliminate various naked except clauses
Idan Kamara <idankk86@gmail.com>
parents: 13986
diff changeset
   152
            except OSError:
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   153
                # failed to make a link, rewrite file
45717
755214a84b9d posix: use context managers in a couple of places
Matt Harbison <matt_harbison@yahoo.com>
parents: 44926
diff changeset
   154
                with open(f, b"wb") as fp:
755214a84b9d posix: use context managers in a couple of places
Matt Harbison <matt_harbison@yahoo.com>
parents: 44926
diff changeset
   155
                    fp.write(data)
755214a84b9d posix: use context managers in a couple of places
Matt Harbison <matt_harbison@yahoo.com>
parents: 44926
diff changeset
   156
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   157
        # no chmod needed at this point
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   158
        return
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   159
    if stat.S_ISLNK(s):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   160
        # switch link to file
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   161
        data = os.readlink(f)
31543
c6cbe5720353 posix: use local reference to unlink
Ryan McElroy <rmcelroy@fb.com>
parents: 31513
diff changeset
   162
        unlink(f)
45717
755214a84b9d posix: use context managers in a couple of places
Matt Harbison <matt_harbison@yahoo.com>
parents: 44926
diff changeset
   163
        with open(f, b"wb") as fp:
755214a84b9d posix: use context managers in a couple of places
Matt Harbison <matt_harbison@yahoo.com>
parents: 44926
diff changeset
   164
            fp.write(data)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   165
        s = 0o666 & ~umask  # avoid restatting for chmod
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   166
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
   167
    sx = s & 0o100
32741
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32432
diff changeset
   168
    if st.st_nlink > 1 and bool(x) != bool(sx):
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32432
diff changeset
   169
        # the file is a hardlink, break it
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   170
        with open(f, b"rb") as fp:
32741
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32432
diff changeset
   171
            data = fp.read()
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32432
diff changeset
   172
        unlink(f)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   173
        with open(f, b"wb") as fp:
32741
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32432
diff changeset
   174
            fp.write(data)
c2cb0de25120 chmod: create a new file when flags are set on a hardlinked file
Koen Van Hoof <koen.van_hoof@nokia.com>
parents: 32432
diff changeset
   175
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   176
    if x and not sx:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   177
        # Turn on +x for every +r bit when making a file executable
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   178
        # and obey umask.
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
   179
        os.chmod(f, s | (s & 0o444) >> 2 & ~umask)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   180
    elif not x and sx:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   181
        # Turn off all +x bits
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
   182
        os.chmod(f, s & 0o666)
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   183
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   184
41289
593f6359681d update: fix edge-case with update.atomic-file and read-only files
Boris Feld <boris.feld@octobus.net>
parents: 40821
diff changeset
   185
def copymode(src, dst, mode=None, enforcewritable=False):
15011
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   186
    '''Copy the file mode from the file at path src to dst.
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   187
    If src doesn't exist, we're using mode instead. If mode is None, we're
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   188
    using umask.'''
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   189
    try:
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
   190
        st_mode = os.lstat(src).st_mode & 0o777
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25658
diff changeset
   191
    except OSError as inst:
15011
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   192
        if inst.errno != errno.ENOENT:
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   193
            raise
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   194
        st_mode = mode
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   195
        if st_mode is None:
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   196
            st_mode = ~umask
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
   197
        st_mode &= 0o666
41289
593f6359681d update: fix edge-case with update.atomic-file and read-only files
Boris Feld <boris.feld@octobus.net>
parents: 40821
diff changeset
   198
593f6359681d update: fix edge-case with update.atomic-file and read-only files
Boris Feld <boris.feld@octobus.net>
parents: 40821
diff changeset
   199
    new_mode = st_mode
593f6359681d update: fix edge-case with update.atomic-file and read-only files
Boris Feld <boris.feld@octobus.net>
parents: 40821
diff changeset
   200
593f6359681d update: fix edge-case with update.atomic-file and read-only files
Boris Feld <boris.feld@octobus.net>
parents: 40821
diff changeset
   201
    if enforcewritable:
593f6359681d update: fix edge-case with update.atomic-file and read-only files
Boris Feld <boris.feld@octobus.net>
parents: 40821
diff changeset
   202
        new_mode |= stat.S_IWUSR
593f6359681d update: fix edge-case with update.atomic-file and read-only files
Boris Feld <boris.feld@octobus.net>
parents: 40821
diff changeset
   203
593f6359681d update: fix edge-case with update.atomic-file and read-only files
Boris Feld <boris.feld@octobus.net>
parents: 40821
diff changeset
   204
    os.chmod(dst, new_mode)
15011
5e44e4b3a0a3 util: move copymode into posix.py and windows.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14927
diff changeset
   205
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   206
13879
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   207
def checkexec(path):
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   208
    """
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   209
    Check whether the given path is on a filesystem with UNIX-like exec flags
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   210
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   211
    Requires a directory (like /foo/.hg)
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   212
    """
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   213
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   214
    # VFAT on some Linux versions can flip mode but it doesn't persist
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   215
    # a FS remount. Frequently we can detect it if files are created
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   216
    # with exec bit on.
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   217
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   218
    try:
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   219
        EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   220
        basedir = os.path.join(path, b'.hg')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   221
        cachedir = os.path.join(basedir, b'wcache')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   222
        storedir = os.path.join(basedir, b'store')
40442
bd0874977a5e checkexec: create destination directory if necessary
Boris Feld <boris.feld@octobus.net>
parents: 39909
diff changeset
   223
        if not os.path.exists(cachedir):
bd0874977a5e checkexec: create destination directory if necessary
Boris Feld <boris.feld@octobus.net>
parents: 39909
diff changeset
   224
            try:
bd0874977a5e checkexec: create destination directory if necessary
Boris Feld <boris.feld@octobus.net>
parents: 39909
diff changeset
   225
                # we want to create the 'cache' directory, not the '.hg' one.
bd0874977a5e checkexec: create destination directory if necessary
Boris Feld <boris.feld@octobus.net>
parents: 39909
diff changeset
   226
                # Automatically creating '.hg' directory could silently spawn
bd0874977a5e checkexec: create destination directory if necessary
Boris Feld <boris.feld@octobus.net>
parents: 39909
diff changeset
   227
                # invalid Mercurial repositories. That seems like a bad idea.
bd0874977a5e checkexec: create destination directory if necessary
Boris Feld <boris.feld@octobus.net>
parents: 39909
diff changeset
   228
                os.mkdir(cachedir)
bd0874977a5e checkexec: create destination directory if necessary
Boris Feld <boris.feld@octobus.net>
parents: 39909
diff changeset
   229
                if os.path.exists(storedir):
bd0874977a5e checkexec: create destination directory if necessary
Boris Feld <boris.feld@octobus.net>
parents: 39909
diff changeset
   230
                    copymode(storedir, cachedir)
bd0874977a5e checkexec: create destination directory if necessary
Boris Feld <boris.feld@octobus.net>
parents: 39909
diff changeset
   231
                else:
bd0874977a5e checkexec: create destination directory if necessary
Boris Feld <boris.feld@octobus.net>
parents: 39909
diff changeset
   232
                    copymode(basedir, cachedir)
bd0874977a5e checkexec: create destination directory if necessary
Boris Feld <boris.feld@octobus.net>
parents: 39909
diff changeset
   233
            except (IOError, OSError):
bd0874977a5e checkexec: create destination directory if necessary
Boris Feld <boris.feld@octobus.net>
parents: 39909
diff changeset
   234
                # we other fallback logic triggers
bd0874977a5e checkexec: create destination directory if necessary
Boris Feld <boris.feld@octobus.net>
parents: 39909
diff changeset
   235
                pass
30455
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   236
        if os.path.isdir(cachedir):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   237
            checkisexec = os.path.join(cachedir, b'checkisexec')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   238
            checknoexec = os.path.join(cachedir, b'checknoexec')
30455
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   239
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   240
            try:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   241
                m = os.stat(checkisexec).st_mode
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   242
            except OSError as e:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   243
                if e.errno != errno.ENOENT:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   244
                    raise
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   245
                # checkisexec does not exist - fall through ...
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   246
            else:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   247
                # checkisexec exists, check if it actually is exec
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   248
                if m & EXECFLAGS != 0:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   249
                    # ensure checkisexec exists, check it isn't exec
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   250
                    try:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   251
                        m = os.stat(checknoexec).st_mode
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   252
                    except OSError as e:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   253
                        if e.errno != errno.ENOENT:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   254
                            raise
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   255
                        open(checknoexec, b'w').close()  # might fail
30455
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   256
                        m = os.stat(checknoexec).st_mode
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   257
                    if m & EXECFLAGS == 0:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   258
                        # check-exec is exec and check-no-exec is not exec
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   259
                        return True
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   260
                    # checknoexec exists but is exec - delete it
31543
c6cbe5720353 posix: use local reference to unlink
Ryan McElroy <rmcelroy@fb.com>
parents: 31513
diff changeset
   261
                    unlink(checknoexec)
30455
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   262
                # checkisexec exists but is not exec - delete it
31543
c6cbe5720353 posix: use local reference to unlink
Ryan McElroy <rmcelroy@fb.com>
parents: 31513
diff changeset
   263
                unlink(checkisexec)
30455
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   264
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   265
            # check using one file, leave it as checkisexec
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   266
            checkdir = cachedir
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   267
        else:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   268
            # check directly in path and don't leave checkisexec behind
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   269
            checkdir = path
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   270
            checkisexec = None
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   271
        fh, fn = pycompat.mkstemp(dir=checkdir, prefix=b'hg-checkexec-')
13879
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   272
        try:
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   273
            os.close(fh)
30454
1ce4c2062ab0 posix: simplify checkexec check
Mads Kiilerich <madski@unity3d.com>
parents: 30453
diff changeset
   274
            m = os.stat(fn).st_mode
30455
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   275
            if m & EXECFLAGS == 0:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   276
                os.chmod(fn, m & 0o777 | EXECFLAGS)
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   277
                if os.stat(fn).st_mode & EXECFLAGS != 0:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   278
                    if checkisexec is not None:
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   279
                        os.rename(fn, checkisexec)
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   280
                        fn = None
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   281
                    return True
13879
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   282
        finally:
30455
b324b4e431e5 posix: give checkexec a fast path; keep the check files and test read only
Mads Kiilerich <madski@unity3d.com>
parents: 30454
diff changeset
   283
            if fn is not None:
31543
c6cbe5720353 posix: use local reference to unlink
Ryan McElroy <rmcelroy@fb.com>
parents: 31513
diff changeset
   284
                unlink(fn)
13879
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   285
    except (IOError, OSError):
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   286
        # we don't care, the user probably won't be able to commit anyway
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   287
        return False
5b0a3f6cbead util: move checkexec() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13400
diff changeset
   288
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   289
13890
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   290
def checklink(path):
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   291
    """check whether the given path is on a symlink-capable filesystem"""
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   292
    # mktemp is not racy because symlink creation will fail if the
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   293
    # file already exists
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   294
    while True:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   295
        cachedir = os.path.join(path, b'.hg', b'wcache')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   296
        checklink = os.path.join(cachedir, b'checklink')
30457
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30456
diff changeset
   297
        # try fast path, read only
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30456
diff changeset
   298
        if os.path.islink(checklink):
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30456
diff changeset
   299
            return True
30456
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30455
diff changeset
   300
        if os.path.isdir(cachedir):
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30455
diff changeset
   301
            checkdir = cachedir
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30455
diff changeset
   302
        else:
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30455
diff changeset
   303
            checkdir = path
0d87b1caed92 posix: move checklink test file to .hg/cache
Mads Kiilerich <madski@unity3d.com>
parents: 30455
diff changeset
   304
            cachedir = None
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   305
        name = tempfile.mktemp(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   306
            dir=pycompat.fsdecode(checkdir), prefix=r'checklink-'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   307
        )
31513
53575feed7c0 posix: tiptoe around tempfile module more delicately
Augie Fackler <augie@google.com>
parents: 31512
diff changeset
   308
        name = pycompat.fsencode(name)
22946
77c121da6143 checklink: always close the NamedTemporaryFile
Augie Fackler <raf@durin42.com>
parents: 22781
diff changeset
   309
        try:
30561
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30457
diff changeset
   310
            fd = None
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30457
diff changeset
   311
            if cachedir is None:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   312
                fd = pycompat.namedtempfile(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   313
                    dir=checkdir, prefix=b'hg-checklink-'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   314
                )
38199
cc9aa88792fe py3: wrap tempfile.NamedTemporaryFile() to return bytes fp.name
Yuya Nishihara <yuya@tcha.org>
parents: 38197
diff changeset
   315
                target = os.path.basename(fd.name)
30561
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30457
diff changeset
   316
            else:
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30457
diff changeset
   317
                # create a fixed file to link to; doesn't matter if it
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30457
diff changeset
   318
                # already exists.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   319
                target = b'checklink-target'
32432
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32331
diff changeset
   320
                try:
36954
0585337ea787 cleanup: fix some latent open(path).read() et al calls we previously missed
Augie Fackler <augie@google.com>
parents: 36789
diff changeset
   321
                    fullpath = os.path.join(cachedir, target)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   322
                    open(fullpath, b'w').close()
32432
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32331
diff changeset
   323
                except IOError as inst:
43525
147576a4e6a2 posix: add a pytype suppression
Augie Fackler <augie@google.com>
parents: 43089
diff changeset
   324
                    if (
44349
a0ec05d93c8e cleanup: re-run black on the codebase
Augie Fackler <augie@google.com>
parents: 43554
diff changeset
   325
                        inst[0] == errno.EACCES
a0ec05d93c8e cleanup: re-run black on the codebase
Augie Fackler <augie@google.com>
parents: 43554
diff changeset
   326
                    ):  # pytype: disable=unsupported-operands
32432
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32331
diff changeset
   327
                        # If we can't write to cachedir, just pretend
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32331
diff changeset
   328
                        # that the fs is readonly and by association
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32331
diff changeset
   329
                        # that the fs won't support symlinks. This
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32331
diff changeset
   330
                        # seems like the least dangerous way to avoid
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32331
diff changeset
   331
                        # data loss.
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32331
diff changeset
   332
                        return False
38a2b9d90131 checklink: degrade gracefully on posix when fs is readonly (issue5511)
Augie Fackler <augie@google.com>
parents: 32331
diff changeset
   333
                    raise
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   334
            try:
30561
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30457
diff changeset
   335
                os.symlink(target, name)
30457
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30456
diff changeset
   336
                if cachedir is None:
31543
c6cbe5720353 posix: use local reference to unlink
Ryan McElroy <rmcelroy@fb.com>
parents: 31513
diff changeset
   337
                    unlink(name)
30457
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30456
diff changeset
   338
                else:
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30456
diff changeset
   339
                    try:
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30456
diff changeset
   340
                        os.rename(name, checklink)
8836f13e3c5b posix: give checklink a fast path that cache the check file and is read only
Mads Kiilerich <madski@unity3d.com>
parents: 30456
diff changeset
   341
                    except OSError:
31543
c6cbe5720353 posix: use local reference to unlink
Ryan McElroy <rmcelroy@fb.com>
parents: 31513
diff changeset
   342
                        unlink(name)
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   343
                return True
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   344
            except OSError as inst:
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   345
                # link creation might race, try again
37978
2d919ab6c5b4 posix: use inst.errno instead of inst[0] on OSError instances
Augie Fackler <augie@google.com>
parents: 37460
diff changeset
   346
                if inst.errno == errno.EEXIST:
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   347
                    continue
26889
1aa5083cbebb posix: fix test-permissions regression
Matt Mackall <mpm@selenic.com>
parents: 26883
diff changeset
   348
                raise
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   349
            finally:
30561
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30457
diff changeset
   350
                if fd is not None:
6a672c3b7860 posix: give the cached symlink a real target
Martijn Pieters <mjpieters@fb.com>
parents: 30457
diff changeset
   351
                    fd.close()
26883
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   352
        except AttributeError:
c750ed59892a posix: retry on symlink race in checklink
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   353
            return False
26889
1aa5083cbebb posix: fix test-permissions regression
Matt Mackall <mpm@selenic.com>
parents: 26883
diff changeset
   354
        except OSError as inst:
1aa5083cbebb posix: fix test-permissions regression
Matt Mackall <mpm@selenic.com>
parents: 26883
diff changeset
   355
            # sshfs might report failure while successfully creating the link
37978
2d919ab6c5b4 posix: use inst.errno instead of inst[0] on OSError instances
Augie Fackler <augie@google.com>
parents: 37460
diff changeset
   356
            if inst.errno == errno.EIO and os.path.exists(name):
31543
c6cbe5720353 posix: use local reference to unlink
Ryan McElroy <rmcelroy@fb.com>
parents: 31513
diff changeset
   357
                unlink(name)
26889
1aa5083cbebb posix: fix test-permissions regression
Matt Mackall <mpm@selenic.com>
parents: 26883
diff changeset
   358
            return False
13890
31eb145b50b6 util: move checklink() to posix.py and return False on Windows
Adrian Buehlmann <adrian@cadifra.com>
parents: 13879
diff changeset
   359
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   360
13916
98ee3dd5bab4 path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13890
diff changeset
   361
def checkosfilename(path):
98ee3dd5bab4 path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13890
diff changeset
   362
    '''Check that the base-relative path is a valid filename on this platform.
98ee3dd5bab4 path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13890
diff changeset
   363
    Returns None if the path is ok, or a UI string describing the problem.'''
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   364
    return None  # on posix platforms, every path is ok
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   365
13916
98ee3dd5bab4 path_auditor: check filenames for basic platform validity (issue2755)
Adrian Buehlmann <adrian@cadifra.com>
parents: 13890
diff changeset
   366
35517
c4caf530b1c7 util: add a function to show the mount point of the filesystem
Matt Harbison <matt_harbison@yahoo.com>
parents: 35513
diff changeset
   367
def getfsmountpoint(dirpath):
c4caf530b1c7 util: add a function to show the mount point of the filesystem
Matt Harbison <matt_harbison@yahoo.com>
parents: 35513
diff changeset
   368
    '''Get the filesystem mount point from a directory (best-effort)
c4caf530b1c7 util: add a function to show the mount point of the filesystem
Matt Harbison <matt_harbison@yahoo.com>
parents: 35513
diff changeset
   369
c4caf530b1c7 util: add a function to show the mount point of the filesystem
Matt Harbison <matt_harbison@yahoo.com>
parents: 35513
diff changeset
   370
    Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc.
c4caf530b1c7 util: add a function to show the mount point of the filesystem
Matt Harbison <matt_harbison@yahoo.com>
parents: 35513
diff changeset
   371
    '''
c4caf530b1c7 util: add a function to show the mount point of the filesystem
Matt Harbison <matt_harbison@yahoo.com>
parents: 35513
diff changeset
   372
    return getattr(osutil, 'getfsmountpoint', lambda x: None)(dirpath)
c4caf530b1c7 util: add a function to show the mount point of the filesystem
Matt Harbison <matt_harbison@yahoo.com>
parents: 35513
diff changeset
   373
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   374
35513
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
   375
def getfstype(dirpath):
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
   376
    '''Get the filesystem type name from a directory (best-effort)
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
   377
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
   378
    Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc.
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
   379
    '''
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
   380
    return getattr(osutil, 'getfstype', lambda x: None)(dirpath)
beede158ea8a util: move getfstype() to the platform modules
Matt Harbison <matt_harbison@yahoo.com>
parents: 34647
diff changeset
   381
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   382
14233
659f34b833b9 rename util.set_binary to setbinary
Adrian Buehlmann <adrian@cadifra.com>
parents: 14232
diff changeset
   383
def setbinary(fd):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   384
    pass
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   385
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   386
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   387
def pconvert(path):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   388
    return path
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   389
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   390
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   391
def localpath(path):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   392
    return path
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   393
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   394
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   395
def samefile(fpath1, fpath2):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   396
    """Returns whether path1 and path2 refer to the same file. This is only
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   397
    guaranteed to work for files, not directories."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   398
    return os.path.samefile(fpath1, fpath2)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   399
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   400
10218
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   401
def samedevice(fpath1, fpath2):
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   402
    """Returns whether fpath1 and fpath2 are on the same device. This is only
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   403
    guaranteed to work for files, not directories."""
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   404
    st1 = os.lstat(fpath1)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   405
    st2 = os.lstat(fpath2)
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   406
    return st1.st_dev == st2.st_dev
750b7a4f01f6 Add support for relinking on Windows.
Siddharth Agarwal <sid.bugzilla@gmail.com>
parents: 9549
diff changeset
   407
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   408
15488
6eff984d8e76 dirstate: fix case-folding identity for traditional Unix
Matt Mackall <mpm@selenic.com>
parents: 15353
diff changeset
   409
# os.path.normcase is a no-op, which doesn't help us on non-native filesystems
6eff984d8e76 dirstate: fix case-folding identity for traditional Unix
Matt Mackall <mpm@selenic.com>
parents: 15353
diff changeset
   410
def normcase(path):
6eff984d8e76 dirstate: fix case-folding identity for traditional Unix
Matt Mackall <mpm@selenic.com>
parents: 15353
diff changeset
   411
    return path.lower()
6eff984d8e76 dirstate: fix case-folding identity for traditional Unix
Matt Mackall <mpm@selenic.com>
parents: 15353
diff changeset
   412
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   413
24594
609aa973c01d posix: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24108
diff changeset
   414
# what normcase does to ASCII strings
609aa973c01d posix: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24108
diff changeset
   415
normcasespec = encoding.normcasespecs.lower
609aa973c01d posix: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24108
diff changeset
   416
# fallback normcase function for non-ASCII strings
609aa973c01d posix: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24108
diff changeset
   417
normcasefallback = normcase
609aa973c01d posix: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24108
diff changeset
   418
34647
dacfcdd8b94e codemod: use pycompat.isdarwin
Jun Wu <quark@fb.com>
parents: 34384
diff changeset
   419
if pycompat.isdarwin:
15551
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   420
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   421
    def normcase(path):
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   422
        '''
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   423
        Normalize a filename for OS X-compatible comparison:
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   424
        - escape-encode invalid characters
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   425
        - decompose to NFD
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   426
        - lowercase
23597
7a5bcd471f2e darwin: omit ignorable codepoints when normcase()ing a file path
Augie Fackler <raf@durin42.com>
parents: 22946
diff changeset
   427
        - omit ignored characters [200c-200f, 202a-202e, 206a-206f,feff]
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   428
34146
0fa781320203 doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents: 33657
diff changeset
   429
        >>> normcase(b'UPPER')
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   430
        'upper'
34206
90b0e1639fd4 posix: fix HFS+ normcase doctest to produce valid bytes literals in Python 3
Augie Fackler <raf@durin42.com>
parents: 34146
diff changeset
   431
        >>> normcase(b'Caf\\xc3\\xa9')
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   432
        'cafe\\xcc\\x81'
34206
90b0e1639fd4 posix: fix HFS+ normcase doctest to produce valid bytes literals in Python 3
Augie Fackler <raf@durin42.com>
parents: 34146
diff changeset
   433
        >>> normcase(b'\\xc3\\x89')
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   434
        'e\\xcc\\x81'
34206
90b0e1639fd4 posix: fix HFS+ normcase doctest to produce valid bytes literals in Python 3
Augie Fackler <raf@durin42.com>
parents: 34146
diff changeset
   435
        >>> normcase(b'\\xb8\\xca\\xc3\\xca\\xbe\\xc8.JPG') # issue3918
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   436
        '%b8%ca%c3\\xca\\xbe%c8.jpg'
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   437
        '''
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   438
15551
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   439
        try:
22781
70624fda193d normcase: for darwin, use fast ASCII lower
Siddharth Agarwal <sid0@fb.com>
parents: 22246
diff changeset
   440
            return encoding.asciilower(path)  # exception for non-ASCII
18501
a3b2dc1aa909 OS X: try cheap ascii .lower() in normcase before making full unicode dance
Mads Kiilerich <madski@unity3d.com>
parents: 18442
diff changeset
   441
        except UnicodeDecodeError:
24595
136ab89d61cb darwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24594
diff changeset
   442
            return normcasefallback(path)
136ab89d61cb darwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24594
diff changeset
   443
136ab89d61cb darwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24594
diff changeset
   444
    normcasespec = encoding.normcasespecs.lower
136ab89d61cb darwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24594
diff changeset
   445
136ab89d61cb darwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24594
diff changeset
   446
    def normcasefallback(path):
18501
a3b2dc1aa909 OS X: try cheap ascii .lower() in normcase before making full unicode dance
Mads Kiilerich <madski@unity3d.com>
parents: 18442
diff changeset
   447
        try:
15551
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   448
            u = path.decode('utf-8')
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   449
        except UnicodeDecodeError:
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   450
            # OS X percent-encodes any bytes that aren't valid utf-8
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   451
            s = b''
26876
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   452
            pos = 0
27380
c7129ed280b8 mac: fix percent-encoding of non-utf-8 characters (issue4999)
Matt Mackall <mpm@selenic.com>
parents: 27362
diff changeset
   453
            l = len(path)
26876
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   454
            while pos < l:
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   455
                try:
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   456
                    c = encoding.getutf8char(path, pos)
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   457
                    pos += len(c)
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   458
                except ValueError:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   459
                    c = b'%%%02X' % ord(path[pos : pos + 1])
26876
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   460
                    pos += 1
b8381832ce2b posix: use getutf8char to handle OS X filename percent-escaping
Matt Mackall <mpm@selenic.com>
parents: 26248
diff changeset
   461
                s += c
19131
af3b651505e2 hfs+: rewrite percent-escaper (issue3918)
Matt Mackall <mpm@selenic.com>
parents: 18868
diff changeset
   462
15551
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   463
            u = s.decode('utf-8')
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   464
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   465
        # Decompose then lowercase (HFS+ technote specifies lower)
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43525
diff changeset
   466
        enc = unicodedata.normalize('NFD', u).lower().encode('utf-8')
23597
7a5bcd471f2e darwin: omit ignorable codepoints when normcase()ing a file path
Augie Fackler <raf@durin42.com>
parents: 22946
diff changeset
   467
        # drop HFS+ ignored characters
7a5bcd471f2e darwin: omit ignorable codepoints when normcase()ing a file path
Augie Fackler <raf@durin42.com>
parents: 22946
diff changeset
   468
        return encoding.hfsignoreclean(enc)
15551
1fa41d1f1351 posix: add extended support for OS X path folding
Matt Mackall <mpm@selenic.com>
parents: 15499
diff changeset
   469
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   470
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   471
if pycompat.sysplatform == b'cygwin':
15711
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   472
    # workaround for cygwin, in which mount point part of path is
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   473
    # treated as case sensitive, even though underlying NTFS is case
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   474
    # insensitive.
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   475
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   476
    # default mount points
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   477
    cygwinmountpoints = sorted(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   478
        [b"/usr/bin", b"/usr/lib", b"/cygdrive",], reverse=True
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   479
    )
15711
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   480
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   481
    # use upper-ing as normcase as same as NTFS workaround
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   482
    def normcase(path):
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   483
        pathlen = len(path)
30619
cfe66dcf45c0 py3: replace os.sep with pycompat.ossep (part 2 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30617
diff changeset
   484
        if (pathlen == 0) or (path[0] != pycompat.ossep):
15711
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   485
            # treat as relative
17203
0cb55b5c19a3 util, posix: eliminate encodinglower and encodingupper
Adrian Buehlmann <adrian@cadifra.com>
parents: 16726
diff changeset
   486
            return encoding.upper(path)
15711
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   487
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   488
        # to preserve case of mountpoint part
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   489
        for mp in cygwinmountpoints:
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   490
            if not path.startswith(mp):
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   491
                continue
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   492
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   493
            mplen = len(mp)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   494
            if mplen == pathlen:  # mount point itself
15711
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   495
                return mp
30619
cfe66dcf45c0 py3: replace os.sep with pycompat.ossep (part 2 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30617
diff changeset
   496
            if path[mplen] == pycompat.ossep:
17203
0cb55b5c19a3 util, posix: eliminate encodinglower and encodingupper
Adrian Buehlmann <adrian@cadifra.com>
parents: 16726
diff changeset
   497
                return mp + encoding.upper(path[mplen:])
15711
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   498
17203
0cb55b5c19a3 util, posix: eliminate encodinglower and encodingupper
Adrian Buehlmann <adrian@cadifra.com>
parents: 16726
diff changeset
   499
        return encoding.upper(path)
15711
c51c9dc13a58 cygwin: add cygwin specific normcase logic
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15672
diff changeset
   500
24596
75ea27f1711d cygwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24595
diff changeset
   501
    normcasespec = encoding.normcasespecs.other
75ea27f1711d cygwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24595
diff changeset
   502
    normcasefallback = normcase
75ea27f1711d cygwin: define normcase spec and fallback
Siddharth Agarwal <sid0@fb.com>
parents: 24595
diff changeset
   503
16240
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
   504
    # Cygwin translates native ACLs to POSIX permissions,
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
   505
    # but these translations are not supported by native
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
   506
    # tools, so the exec bit tends to be set erroneously.
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
   507
    # Therefore, disable executable bit access on Cygwin.
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
   508
    def checkexec(path):
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
   509
        return False
95e45abe7e8e posix: ignore execution bit in cygwin (issue3301)
A. S. Budden <abudden@gmail.com>
parents: 15791
diff changeset
   510
16241
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
   511
    # Similarly, Cygwin's symlink emulation is likely to create
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
   512
    # problems when Mercurial is used from both Cygwin and native
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
   513
    # Windows, with other native tools, or on shared volumes
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
   514
    def checklink(path):
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
   515
        return False
60cc3a0d2249 posix: disable cygwin's symlink emulation
Matt Mackall <mpm@selenic.com>
parents: 16240
diff changeset
   516
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   517
23683
5edb387158a1 posix: quote the specified string only when it may have to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23597
diff changeset
   518
_needsshellquote = None
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   519
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   520
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   521
def shellquote(s):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   522
    if pycompat.sysplatform == b'OpenVMS':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   523
        return b'"%s"' % s
23683
5edb387158a1 posix: quote the specified string only when it may have to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23597
diff changeset
   524
    global _needsshellquote
5edb387158a1 posix: quote the specified string only when it may have to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23597
diff changeset
   525
    if _needsshellquote is None:
31498
492c64afc54c py3: make the regular expression bytes to prevent TypeError
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30697
diff changeset
   526
        _needsshellquote = re.compile(br'[^a-zA-Z0-9._/+-]').search
24108
d65ecb814fc0 shellquote: fix missing quotes for empty string
Yuya Nishihara <yuya@tcha.org>
parents: 23683
diff changeset
   527
    if s and not _needsshellquote(s):
23683
5edb387158a1 posix: quote the specified string only when it may have to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23597
diff changeset
   528
        # "s" shouldn't have to be quoted
5edb387158a1 posix: quote the specified string only when it may have to be quoted
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 23597
diff changeset
   529
        return s
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   530
    else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   531
        return b"'%s'" % s.replace(b"'", b"'\\''")
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   532
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   533
36445
0cb09c322647 util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents: 36336
diff changeset
   534
def shellsplit(s):
0cb09c322647 util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents: 36336
diff changeset
   535
    """Parse a command string in POSIX shell way (best-effort)"""
0cb09c322647 util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents: 36336
diff changeset
   536
    return pycompat.shlexsplit(s, posix=True)
0cb09c322647 util: factor out shellsplit() function
Yuya Nishihara <yuya@tcha.org>
parents: 36336
diff changeset
   537
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   538
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   539
def testpid(pid):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   540
    '''return False if pid dead, True if running or not sure'''
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   541
    if pycompat.sysplatform == b'OpenVMS':
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   542
        return True
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   543
    try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   544
        os.kill(pid, 0)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   545
        return True
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25658
diff changeset
   546
    except OSError as inst:
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   547
        return inst.errno != errno.ESRCH
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   548
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   549
8657
3fa92c618624 posix: do not use fstat in isowner
Martin Geisler <mg@lazybytes.net>
parents: 8614
diff changeset
   550
def isowner(st):
3fa92c618624 posix: do not use fstat in isowner
Martin Geisler <mg@lazybytes.net>
parents: 8614
diff changeset
   551
    """Return True if the stat object st is from the current user."""
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   552
    return st.st_uid == os.getuid()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   553
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   554
14271
4030630fb59c rename util.find_exe to findexe
Adrian Buehlmann <adrian@cadifra.com>
parents: 14237
diff changeset
   555
def findexe(command):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   556
    '''Find executable for command searching like which does.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   557
    If command is a basename then PATH is searched for command.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   558
    PATH isn't searched if command is an absolute or relative path.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   559
    If command isn't found None is returned.'''
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   560
    if pycompat.sysplatform == b'OpenVMS':
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   561
        return command
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   562
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   563
    def findexisting(executable):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   564
        b'Will return executable if existing file'
15499
58f96703a9ab posix: fix findexe() to check for file type and access
Marc-Antoine Ruel <maruel@google.com>
parents: 15488
diff changeset
   565
        if os.path.isfile(executable) and os.access(executable, os.X_OK):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   566
            return executable
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   567
        return None
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   568
30619
cfe66dcf45c0 py3: replace os.sep with pycompat.ossep (part 2 of 4)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 30617
diff changeset
   569
    if pycompat.ossep in command:
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   570
        return findexisting(command)
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   571
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   572
    if pycompat.sysplatform == b'plan9':
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   573
        return findexisting(os.path.join(b'/bin', command))
16383
f5dd179bfa4a plan9: initial support for plan 9 from bell labs
Steven Stallion <sstallion@gmail.com>
parents: 16241
diff changeset
   574
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   575
    for path in encoding.environ.get(b'PATH', b'').split(pycompat.ospathsep):
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   576
        executable = findexisting(os.path.join(path, command))
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   577
        if executable is not None:
15499
58f96703a9ab posix: fix findexe() to check for file type and access
Marc-Antoine Ruel <maruel@google.com>
parents: 15488
diff changeset
   578
            return executable
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   579
    return None
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   580
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   581
14237
4d684d8210a1 rename util.set_signal_handler to setsignalhandler
Adrian Buehlmann <adrian@cadifra.com>
parents: 14235
diff changeset
   582
def setsignalhandler():
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   583
    pass
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   584
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   585
32331
bd872f64a8ba cleanup: use set literals
Martin von Zweigbergk <martinvonz@google.com>
parents: 32182
diff changeset
   586
_wantedkinds = {stat.S_IFREG, stat.S_IFLNK}
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   587
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   588
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   589
def statfiles(files):
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   590
    '''Stat each file in files. Yield each stat, or None if a file does not
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   591
    exist or has a type we don't care about.'''
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   592
    lstat = os.lstat
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   593
    getkind = stat.S_IFMT
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   594
    for nf in files:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   595
        try:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   596
            st = lstat(nf)
18017
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   597
            if getkind(st.st_mode) not in _wantedkinds:
74912fe3d718 dirstate: move file type filtering to its source
Bryan O'Sullivan <bryano@fb.com>
parents: 17560
diff changeset
   598
                st = None
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25658
diff changeset
   599
        except OSError as err:
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   600
            if err.errno not in (errno.ENOENT, errno.ENOTDIR):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   601
                raise
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   602
            st = None
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   603
        yield st
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   604
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   605
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   606
def getuser():
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   607
    '''return name of current user'''
32182
f0ea0f642627 py3: make posix.getuser return a bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 31545
diff changeset
   608
    return pycompat.fsencode(getpass.getuser())
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   609
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   610
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   611
def username(uid=None):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   612
    """Return the name of the user with the given uid.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   613
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   614
    If uid is None, return the name of the current user."""
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   615
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   616
    if uid is None:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   617
        uid = os.getuid()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   618
    try:
38264
a4aa77b84efd py3: make sure util.username() always returns bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 38199
diff changeset
   619
        return pycompat.fsencode(pwd.getpwuid(uid)[0])
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   620
    except KeyError:
38264
a4aa77b84efd py3: make sure util.username() always returns bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 38199
diff changeset
   621
        return b'%d' % uid
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   622
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   623
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   624
def groupname(gid=None):
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   625
    """Return the name of the group with the given gid.
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   626
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   627
    If gid is None, return the name of the current group."""
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   628
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   629
    if gid is None:
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   630
        gid = os.getgid()
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   631
    try:
41832
faa04f45b5fe py3: make sure return value of posix.groupname() is bytes
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41537
diff changeset
   632
        return pycompat.fsencode(grp.getgrgid(gid)[0])
7890
e710f0f592b2 util: split out posix, windows, and win32 modules
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   633
    except KeyError:
41832
faa04f45b5fe py3: make sure return value of posix.groupname() is bytes
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41537
diff changeset
   634
        return pycompat.bytestr(gid)
10237
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
   635
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   636
11138
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
   637
def groupmembers(name):
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
   638
    """Return the list of members of the group with the given
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
   639
    name, KeyError if the group does not exist.
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
   640
    """
41537
3ef8bec91465 py3: do a fsdecode(), fsencode() dance in posix.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41518
diff changeset
   641
    name = pycompat.fsdecode(name)
3ef8bec91465 py3: do a fsdecode(), fsencode() dance in posix.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 41518
diff changeset
   642
    return pycompat.rapply(pycompat.fsencode, list(grp.getgrnam(name).gr_mem))
11138
99eee847beaa acl: grp module is not available on windows
Patrick Mezard <pmezard@gmail.com>
parents: 11011
diff changeset
   643
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   644
10237
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
   645
def spawndetached(args):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   646
    return os.spawnvp(os.P_NOWAIT | getattr(os, 'P_DETACH', 0), args[0], args)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   647
10237
2f7a38f336f4 serve: add and use portable spawnvp replacement
Patrick Mezard <pmezard@gmail.com>
parents: 10218
diff changeset
   648
10239
8e4be44a676f Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   649
def gethgcmd():
8e4be44a676f Find right hg command for detached process
Patrick Mezard <pmezard@gmail.com>
parents: 10237
diff changeset
   650
    return sys.argv[:1]
11010
18e81d42ee5c util: fix default termwidth() under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 10264
diff changeset
   651
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   652
14908
e2b5605501df util: move "default" makedir to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14273
diff changeset
   653
def makedir(path, notindexed):
e2b5605501df util: move "default" makedir to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14273
diff changeset
   654
    os.mkdir(path)
14909
c627fe32c923 util: move "default" unlinkpath to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14908
diff changeset
   655
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   656
14910
570ea0259b0a util: move "default" lookupreg to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14909
diff changeset
   657
def lookupreg(key, name=None, scope=None):
570ea0259b0a util: move "default" lookupreg to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14909
diff changeset
   658
    return None
14911
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
   659
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   660
14911
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
   661
def hidewindow():
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
   662
    """Hide current shell window.
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
   663
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
   664
    Used to hide the window opened when starting asynchronous
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
   665
    child process under Windows, unneeded on other systems.
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
   666
    """
5b39503157fd util: move "default" hidewindow to posix.py
Adrian Buehlmann <adrian@cadifra.com>
parents: 14910
diff changeset
   667
    pass
14926
4e7e63fc685a util: eliminate wildcard imports
Adrian Buehlmann <adrian@cadifra.com>
parents: 14911
diff changeset
   668
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   669
14927
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   670
class cachestat(object):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   671
    def __init__(self, path):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   672
        self.stat = os.stat(path)
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   673
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   674
    def cacheable(self):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   675
        return bool(self.stat.st_ino)
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   676
15791
a814f8fcc65a Use explicit integer division
Martin Geisler <mg@aragost.com>
parents: 15711
diff changeset
   677
    __hash__ = object.__hash__
a814f8fcc65a Use explicit integer division
Martin Geisler <mg@aragost.com>
parents: 15711
diff changeset
   678
14927
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   679
    def __eq__(self, other):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   680
        try:
18442
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   681
            # Only dev, ino, size, mtime and atime are likely to change. Out
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   682
            # of these, we shouldn't compare atime but should compare the
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   683
            # rest. However, one of the other fields changing indicates
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   684
            # something fishy going on, so return False if anything but atime
ecba9b0e7672 posix: don't compare atime when determining if a file has changed
Siddharth Agarwal <sid0@fb.com>
parents: 18288
diff changeset
   685
            # changes.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   686
            return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   687
                self.stat.st_mode == other.stat.st_mode
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   688
                and self.stat.st_ino == other.stat.st_ino
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   689
                and self.stat.st_dev == other.stat.st_dev
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   690
                and self.stat.st_nlink == other.stat.st_nlink
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   691
                and self.stat.st_uid == other.stat.st_uid
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   692
                and self.stat.st_gid == other.stat.st_gid
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   693
                and self.stat.st_size == other.stat.st_size
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   694
                and self.stat[stat.ST_MTIME] == other.stat[stat.ST_MTIME]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   695
                and self.stat[stat.ST_CTIME] == other.stat[stat.ST_CTIME]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   696
            )
14927
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   697
        except AttributeError:
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   698
            return False
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   699
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   700
    def __ne__(self, other):
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   701
        return not self == other
2aa3e07b2f07 posix, windows: introduce cachestat
Idan Kamara <idankk86@gmail.com>
parents: 14926
diff changeset
   702
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   703
18868
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
   704
def statislink(st):
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
   705
    '''check whether a stat result is a symlink'''
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
   706
    return st and stat.S_ISLNK(st.st_mode)
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
   707
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   708
18868
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
   709
def statisexec(st):
cafa447a7d3b util: add functions to check symlink/exec bits
Bryan O'Sullivan <bryano@fb.com>
parents: 18501
diff changeset
   710
    '''check whether a stat result is an executable file'''
25658
e93036747902 global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25420
diff changeset
   711
    return st and (st.st_mode & 0o100 != 0)
22245
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   712
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   713
25420
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   714
def poll(fds):
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   715
    """block until something happens on any file descriptor
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   716
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   717
    This is a generic helper that will check for any activity
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   718
    (read, write.  exception) and return the list of touched files.
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   719
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   720
    In unsupported cases, it will raise a NotImplementedError"""
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   721
    try:
30262
5f33116cd787 posix: make poll() restart on interruption by signal (issue5452)
Yuya Nishihara <yuya@tcha.org>
parents: 29530
diff changeset
   722
        while True:
5f33116cd787 posix: make poll() restart on interruption by signal (issue5452)
Yuya Nishihara <yuya@tcha.org>
parents: 29530
diff changeset
   723
            try:
5f33116cd787 posix: make poll() restart on interruption by signal (issue5452)
Yuya Nishihara <yuya@tcha.org>
parents: 29530
diff changeset
   724
                res = select.select(fds, fds, fds)
5f33116cd787 posix: make poll() restart on interruption by signal (issue5452)
Yuya Nishihara <yuya@tcha.org>
parents: 29530
diff changeset
   725
                break
5f33116cd787 posix: make poll() restart on interruption by signal (issue5452)
Yuya Nishihara <yuya@tcha.org>
parents: 29530
diff changeset
   726
            except select.error as inst:
5f33116cd787 posix: make poll() restart on interruption by signal (issue5452)
Yuya Nishihara <yuya@tcha.org>
parents: 29530
diff changeset
   727
                if inst.args[0] == errno.EINTR:
5f33116cd787 posix: make poll() restart on interruption by signal (issue5452)
Yuya Nishihara <yuya@tcha.org>
parents: 29530
diff changeset
   728
                    continue
5f33116cd787 posix: make poll() restart on interruption by signal (issue5452)
Yuya Nishihara <yuya@tcha.org>
parents: 29530
diff changeset
   729
                raise
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   730
    except ValueError:  # out of range file descriptor
25420
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   731
        raise NotImplementedError()
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   732
    return sorted(list(set(sum(res, []))))
c2ec81891502 util: add a simple poll utility
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 24692
diff changeset
   733
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   734
22245
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   735
def readpipe(pipe):
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   736
    """Read all available data from a pipe."""
22246
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   737
    # We can't fstat() a pipe because Linux will always report 0.
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   738
    # So, we set the pipe to non-blocking mode and read everything
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   739
    # that's available.
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   740
    flags = fcntl.fcntl(pipe, fcntl.F_GETFL)
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   741
    flags |= os.O_NONBLOCK
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   742
    oldflags = fcntl.fcntl(pipe, fcntl.F_SETFL, flags)
22245
234e4c24b980 platform: implement readpipe()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 20202
diff changeset
   743
22246
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   744
    try:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   745
        chunks = []
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   746
        while True:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   747
            try:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   748
                s = pipe.read()
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   749
                if not s:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   750
                    break
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   751
                chunks.append(s)
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   752
            except IOError:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   753
                break
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   754
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   755
        return b''.join(chunks)
22246
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   756
    finally:
331cbf088c4c posix: implement readpipe using non-blocking I/O (issue4336)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 22245
diff changeset
   757
        fcntl.fcntl(pipe, fcntl.F_SETFL, oldflags)
29530
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   758
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42189
diff changeset
   759
29530
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   760
def bindunixsocket(sock, path):
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   761
    """Bind the UNIX domain socket to the specified path"""
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   762
    # use relative path instead of full path at bind() if possible, since
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   763
    # AF_UNIX path has very small length limit (107 chars) on common
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   764
    # platforms (see sys/un.h)
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   765
    dirname, basename = os.path.split(path)
3239e2fdd2e2 chgserver: extract utility to bind unix domain socket to long path
Yuya Nishihara <yuya@tcha.org>
parents: 27380
diff changeset
   766
    bakwdfd = None
45718
87c35b5a14eb posix: avoid a leaked file descriptor in a unix domain socket exception case
Matt Harbison <matt_harbison@yahoo.com>
parents: 45717
diff changeset
   767
87c35b5a14eb posix: avoid a leaked file descriptor in a unix domain socket exception case
Matt Harbison <matt_harbison@yahoo.com>
parents: 45717
diff changeset
   768
    try:
87c35b5a14eb posix: avoid a leaked file descriptor in a unix domain socket exception case
Matt Harbison <matt_harbison@yahoo.com>
parents: 45717
diff changeset
   769
        if dirname:
87c35b5a14eb posix: avoid a leaked file descriptor in a unix domain socket exception case
Matt Harbison <matt_harbison@yahoo.com>
parents: 45717
diff changeset
   770
            bakwdfd = os.open(b'.', os.O_DIRECTORY)
87c35b5a14eb posix: avoid a leaked file descriptor in a unix domain socket exception case
Matt Harbison <matt_harbison@yahoo.com>
parents: 45717
diff changeset
   771
            os.chdir(dirname)
87c35b5a14eb posix: avoid a leaked file descriptor in a unix domain socket exception case
Matt Harbison <matt_harbison@yahoo.com>
parents: 45717
diff changeset
   772
        sock.bind(basename)
87c35b5a14eb posix: avoid a leaked file descriptor in a unix domain socket exception case
Matt Harbison <matt_harbison@yahoo.com>
parents: 45717
diff changeset
   773
        if bakwdfd:
87c35b5a14eb posix: avoid a leaked file descriptor in a unix domain socket exception case
Matt Harbison <matt_harbison@yahoo.com>
parents: 45717
diff changeset
   774
            os.fchdir(bakwdfd)
87c35b5a14eb posix: avoid a leaked file descriptor in a unix domain socket exception case
Matt Harbison <matt_harbison@yahoo.com>
parents: 45717
diff changeset
   775
    finally:
87c35b5a14eb posix: avoid a leaked file descriptor in a unix domain socket exception case
Matt Harbison <matt_harbison@yahoo.com>
parents: 45717
diff changeset
   776
        if bakwdfd:
87c35b5a14eb posix: avoid a leaked file descriptor in a unix domain socket exception case
Matt Harbison <matt_harbison@yahoo.com>
parents: 45717
diff changeset
   777
            os.close(bakwdfd)